簡體   English   中英

使用Comparator對地圖進行排序

[英]Sorting Map using Comparator

我正在嘗試使用Comparator根據序列在TreeMap實現排序。

    final String sequence="People,Object,Environment,Message,Service";
Comparator<String> comparator = new Comparator<String>() {
             @Override
             public int compare(String key1, String key2) {
                 int returned = sequence.indexOf(key1) - sequence.indexOf(key2);

                 if (returned == 0 && !key1.contains(key2))
                     returned = -1;

                 return returned;

             }
         };
List<String> list=new ArrayList<String>();
          Map<String,String> lhm = new TreeMap<String,String>(comparator);
       // Put elements to the map
          lhm.put("Object", "biu");
          lhm.put("Message", "nuios");
          lhm.put("Service", "sdfe");
          lhm.put("People", "dfdfh");
          lhm.put("Environment", "qwe");
          lhm.put("Other", "names");
          lhm.put("Elements", "ioup");          
          lhm.put("Rand", "uiy");
for(Entry<String, String> entry : lhm.entrySet()) {
                System.out.println(entry.getKey());
            }

我在這里得到的輸出是

Rand
Elements
Other
People
Object
Environment
Message
Service

樹圖中等於序列的元素是正確排序的,但不遵循序列的其他元素應該在序列之后。我的期望如下

People
Object
Environment
Message
Service
Rand
Elements
Other

怎么實現這個?

假設如果我在TreeMap的元素中添加更多單詞意味着我的Comparator甚至沒有對元素進行排序。就像這樣

lhm.put("Object IOn", "biu");
          lhm.put("Message dfb", "nuios");
          lhm.put("Serviceabc", "sdfe");
          lhm.put("Peoplexxx", "dfdfh");
          lhm.put("Environmentxxx", "qwe");
          lhm.put("Other", "names");
          lhm.put("Elements", "ioup");          
          lhm.put("Rand", "uiy");

我的輸出變成了

Rand
Elements
Other
Environmentxxx
Peoplexxx
Serviceabc
Message dfb
Object IOn

有人幫我改寫我的Comparator來解決這個問題嗎?

這是應該完成的一些簡單代碼。

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class FixedOrderComparator implements Comparator<String> {

  private final Map<String, Integer> index = new HashMap<>();

  public FixedOrderComparator(String elements) {
    String[] split = elements.split(",");
    for (int i = 0; i < split.length; i++) {
      index.put(split[i], i);
    }
  }

  @Override
  public int compare(String left, String right) {
    Integer rankLeft = index.get(left);
    Integer rankRight = index.get(right);
    if (rankLeft != null && rankRight != null) {
      return rankLeft.compareTo(rankRight);
    }
    if (rankLeft == null && rankRight == null) {
      return left.compareTo(right);
    }
    return Boolean.compare(rankLeft == null, rankRight == null);
  }

}

您必須更正比較器中使用的邏輯。

  final String sequence="People,Object,Environment,Message,Service";
  System.out.println(sequence.indexOf("People"));  // 0
  System.out.println(sequence.indexOf("Object"));  // 7
  System.out.println(sequence.indexOf("Message"));  // 26
  System.out.println(sequence.indexOf("Environment")); // 14

.indexOf(key1)返回first character of the Stringfirst character of the String的索引而不是String

 int returned = sequence.indexOf(key1) - sequence.indexOf(key2);
 if(returned < 0){ 
    // then it is sorted; 
    return 1;
 }
 else{ return -1; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM