简体   繁体   English

Java TreeMap Comparator

[英]Java TreeMap Comparator

I need a comparator for a TreeMap. 我需要一个TreeMap的比较器。 Should I write this anonymously in the constructor for my TreeMap? 我应该在我的TreeMap的构造函数中匿名写这个吗? How else could I write my comparator. 我怎么能写我的比较器。 Currently, Java does not like my code (can I do this anonymously?): 目前,Java不喜欢我的代码(我可以匿名执行此操作吗?):

SortedMap<String, Double> myMap = 
    new TreeMap<String, Double>(new Comparator<Entry<String, Double>>()
    {
        public int compare(Entry<String, Double> o1, Entry<String, Double> o2)
        {
            return o1.getValue().compareTo(o2.getValue());
        } 
    });
  1. Can I do the above anonymously? 我可以匿名进行上述操作吗?
  2. How else could I do this? 我怎么能这样做?
  3. I want to sort myMap by the Value not the Key 我想通过Value而不是Key对myMap进行排序

You can not sort TreeMap on values. 您无法在值上对TreeMap进行排序。

A Red-Black tree based NavigableMap implementation. 基于红黑树的NavigableMap实现。 The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used You will need to provide comparator for Comparator<? super K> 地图根据其键的自然顺序进行排序,或者按照地图创建时提供的comparator进行排序,具体取决于使用的构造函数。您需要为Comparator<? super K>提供comparator Comparator<? super K> Comparator<? super K> so your comparator should compare on keys. Comparator<? super K>所以你的比较器应该在键上进行比较。

To provide sort on values you will need SortedSet . 要对值进行排序,您需要使用SortedSet Use 使用

SortedSet<Map.Entry<String, Double>> sortedset = new TreeSet<Map.Entry<String, Double>>(
            new Comparator<Map.Entry<String, Double>>() {
                @Override
                public int compare(Map.Entry<String, Double> e1,
                        Map.Entry<String, Double> e2) {
                    return e1.getValue().compareTo(e2.getValue());
                }
            });

  sortedset.addAll(myMap.entrySet());

To give you an example 举个例子

    SortedMap<String, Double> myMap = new TreeMap<String, Double>();
    myMap.put("a", 10.0);
    myMap.put("b", 9.0);
    myMap.put("c", 11.0);
    myMap.put("d", 2.0);
    sortedset.addAll(myMap.entrySet());
    System.out.println(sortedset);

Output: 输出:

  [d=2.0, b=9.0, a=10.0, c=11.0]

The comparator should be only for the key, not for the whole entry. 比较器应仅用于键,而不是整个条目。 It sorts the entries based on the keys. 它根据键对条目进行排序。

You should change it to something as follows 您应该将其更改为以下内容

SortedMap<String, Double> myMap = 
    new TreeMap<String, Double>(new Comparator<String>()
    {
        public int compare(String o1, String o2)
        {
            return o1.compareTo(o2);
        } 
});

Update 更新

You can do something as follows (create a list of entries in the map and sort the list base on value, but note this not going to sort the map itself) - 您可以执行以下操作(在地图中创建条目列表并根据值对列表进行排序,但请注意,这不会对地图本身进行排序) -

List<Map.Entry<String, Double>> entryList = new ArrayList<Map.Entry<String, Double>>(myMap.entrySet());
    Collections.sort(entryList, new Comparator<Map.Entry<String, Double>>() {
        @Override
        public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    });

you can swipe the key and the value. 你可以刷键和值。 For example 例如

        String[] k = {"Elena", "Thomas", "Hamilton", "Suzie", "Phil"};
        int[] v = {341, 273, 278, 329, 445};
        TreeMap<Integer,String>a=new TreeMap();
        for (int i = 0; i < k.length; i++) 
           a.put(v[i],k[i]);            
        System.out.println(a.firstEntry().getValue()+"\t"+a.firstEntry().getKey());
        a.remove(a.firstEntry().getKey());
        System.out.println(a.firstEntry().getValue()+"\t"+a.firstEntry().getKey());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM