使用Comparator对地图进行排序 [英] Sorting Map using Comparator

查看:177
本文介绍了使用Comparator对地图进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Comparator 根据序列在 TreeMap 中实现排序。

I'm trying Comparator to implement a sort in TreeMap according to a sequence.

    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

树形图中等于序列的元素是正确排序的,但是其他元素没有跟随序列应该在序列之后。我的期望如下:

The Elements in treemap which equals the sequence are ordered correctly but other elements which are not following the sequence should come after the sequence.My expectation is like following

People
Object
Environment
Message
Service
Rand
Elements
Other

如何实现这个?

假设如果我在TreeMap的元素中添加更多的单词意味着我的Comparator甚至没有对元素进行排序。就像这样

Suppose If I add some more words to the elements of my TreeMap means my Comparator doesn't even order the elements.Like this

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来解决这个问题吗?

Somebody help me to rewrite my Comparator to fix this problem?

推荐答案

以下是该任务的一些简单代码。

Here is some simple code that should to the task.

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

}

这篇关于使用Comparator对地图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆