3

我正在尝试根据顺序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

有人帮我重写我的比较器来解决这个问题吗?

4

2 回答 2

1

这是一些应该完成任务的简单代码。

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);
  }

}
于 2014-05-29T12:29:31.383 回答
0

您必须更正比较器中使用的逻辑。

  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 String而不是String.

 int returned = sequence.indexOf(key1) - sequence.indexOf(key2);
 if(returned < 0){ 
    // then it is sorted; 
    return 1;
 }
 else{ return -1; }
于 2013-09-10T14:05:25.083 回答