Java 树映射按值排序

Java 树映射按值排序,java,Java,我想写一个比较器,它可以让我按照值而不是默认的自然顺序对树映射进行排序 我试过这样的方法,但没能找出哪里出了问题: import java.util.*; class treeMap { public static void main(String[] args) { System.out.println("the main"); byValue cmp = new byValue(); Map<String, Integer>

我想写一个比较器,它可以让我按照值而不是默认的自然顺序对树映射进行排序

我试过这样的方法,但没能找出哪里出了问题:

import java.util.*;

class treeMap {
    public static void main(String[] args) {
        System.out.println("the main");
        byValue cmp = new byValue();
        Map<String, Integer> map = new TreeMap<String, Integer>(cmp);
        map.put("de",10);
        map.put("ab", 20);
        map.put("a",5);

        for (Map.Entry<String,Integer> pair: map.entrySet()) {
            System.out.println(pair.getKey()+":"+pair.getValue());
        }
    }
}

class byValue implements Comparator<Map.Entry<String,Integer>> {
    public int compare(Map.Entry<String,Integer> e1, Map.Entry<String,Integer> e2) {
        if (e1.getValue() < e2.getValue()){
            return 1;
        } else if (e1.getValue() == e2.getValue()) {
            return 0;
        } else {
            return -1;
        }
    }
}
import java.util.*;
类树映射{
公共静态void main(字符串[]args){
System.out.println(“主”);
byValue cmp=新的byValue();
Map Map=新树映射(cmp);
地图.put("de",10);;
地图放置(“ab”,20);
地图放置(“a”,5);
for(Map.Entry对:Map.entrySet()){
System.out.println(pair.getKey()+“:”+pair.getValue());
}
}
}
类byValue实现比较器{
公共整数比较(Map.Entry e1,Map.Entry e2){
if(e1.getValue()

我想我要问的是:我能得到一个
映射吗?条目
传递给比较器吗?

这不能通过使用
比较器来完成,因为它总是会得到映射的键来进行比较
TreeMap
只能按键排序。

A
TreeMap
始终按键排序,其他任何操作都是不可能的。
比较器
仅允许您控制键的排序方式


如果需要排序的值,必须将它们提取到
列表中并进行排序。

不能让
树映射本身对值进行排序,因为这违反了规范:

一个
映射
,进一步提供其键的总顺序

但是,使用外部集合,您始终可以按照自己的意愿进行排序,可以通过键、值,甚至是两者的组合(!!)

这里有一个通用方法,它返回
映射的
分类集
。给定一个
映射
,其值为
可比

static <K,V extends Comparable<? super V>>
SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
    SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
        new Comparator<Map.Entry<K,V>>() {
            @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                int res = e1.getValue().compareTo(e2.getValue());
                return res != 0 ? res : 1;
            }
        }
    );
    sortedEntries.addAll(map.entrySet());
    return sortedEntries;
}
相关问题
  • (不!!!)
  • (是的!!!)

    • 多基因润滑油的答案几乎是完美的。不过它有一个重要的bug。它不会处理值相同的映射条目

      此代码:

      Map<String, Integer> nonSortedMap = new HashMap<String, Integer>();
      nonSortedMap.put("ape", 1);
      nonSortedMap.put("pig", 3);
      nonSortedMap.put("cow", 1);
      nonSortedMap.put("frog", 2);
      
      for (Entry<String, Integer> entry  : entriesSortedByValues(nonSortedMap)) {
          System.out.println(entry.getKey()+":"+entry.getValue());
      }
      
      注意我们的母牛是如何消失的,因为它与我们的猿共享值“1”:哦

      此代码的修改解决了该问题:

      static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
              SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
                  new Comparator<Map.Entry<K,V>>() {
                      @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                          int res = e1.getValue().compareTo(e2.getValue());
                          return res != 0 ? res : 1; // Special fix to preserve items with equal values
                      }
                  }
              );
              sortedEntries.addAll(map.entrySet());
              return sortedEntries;
          }
      

      static很多人听人建议使用列表,我也更喜欢使用它

      这里有两种方法,您需要根据它们的值对映射的条目进行排序

          static final Comparator<Entry<?, Double>> DOUBLE_VALUE_COMPARATOR = 
              new Comparator<Entry<?, Double>>() {
                  @Override
                  public int compare(Entry<?, Double> o1, Entry<?, Double> o2) {
                      return o1.getValue().compareTo(o2.getValue());
                  }
              };
      
              static final List<Entry<?, Double>> sortHashMapByDoubleValue(HashMap temp)
              {
                  Set<Entry<?, Double>> entryOfMap = temp.entrySet();
      
                  List<Entry<?, Double>> entries = new ArrayList<Entry<?, Double>>(entryOfMap);
                  Collections.sort(entries, DOUBLE_VALUE_COMPARATOR);
                  return entries;
              }
      

      静态最终比较器(staticfinalcomparator)的答案是好的,但它还需要一件事才能完美。在他回答下面的评论中,dacwe(正确地)指出他的实现违反了集合的比较/等于契约。如果您尝试调用集合中明确存在的条目的contains或remove,集合将无法识别它,因为代码允许将具有相同值的条目放置在集合中。因此,为了解决这个问题,我们需要测试键之间的相等性:

      static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
          SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
              new Comparator<Map.Entry<K,V>>() {
                  @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                      int res = e1.getValue().compareTo(e2.getValue());
                      if (e1.getKey().equals(e2.getKey())) {
                          return res; // Code will now handle equality properly
                      } else {
                          return res != 0 ? res : 1; // While still adding all entries
                      }
                  }
              }
          );
          sortedEntries.addAll(map.entrySet());
          return sortedEntries;
      }
      
      Java 8中的静态
      static:

      LinkedHashMap<Integer, String> sortedMap = 
          map.entrySet().stream().
          sorted(Entry.comparingByValue()).
          collect(Collectors.toMap(Entry::getKey, Entry::getValue,
                                   (e1, e2) -> e1, LinkedHashMap::new));
      
      LinkedHashMap-sortedMap=
      map.entrySet().stream()。
      已排序(Entry.comparingByValue())。
      collect(Collectors.toMap)(条目::getKey,条目::getValue,
      (e1,e2)->e1,LinkedHashMap::new);
      
      我知道这篇文章特别要求按值对树映射进行排序,但对于我们这些不太关心实现,但确实希望在添加元素时保持集合排序的解决方案的人,我将非常感谢对这个基于树集的解决方案的反馈。首先,元素不容易通过键检索,但对于我手头的用例(找到具有最低值的n个键),这不是一个要求

        TreeSet<Map.Entry<Integer, Double>> set = new TreeSet<>(new Comparator<Map.Entry<Integer, Double>>()
        {
          @Override
          public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2)
          {
            int valueComparison = o1.getValue().compareTo(o2.getValue());
            return valueComparison == 0 ? o1.getKey().compareTo(o2.getKey()) : valueComparison;
          }
        });
        int key = 5;
        double value = 1.0;
        set.add(new AbstractMap.SimpleEntry<>(key, value));
      
      TreeSet集=新树集(新比较器()
      {
      @凌驾
      公共整数比较(Map.Entry o1,Map.Entry o2)
      {
      int-valueComparison=o1.getValue().comparieto(o2.getValue());
      返回值比较==0?o1.getKey().compareTo(o2.getKey()):值比较;
      }
      });
      int键=5;
      双值=1.0;
      add(newAbstractMap.SimpleEntry(key,value));
      
      导入java.util.*;
      公共班机{
      公共静态void main(字符串[]args){
      TreeMap initTree=newtreemap();
      initTree.put(“D”,0);
      initTree.put(“C”,-3);
      initTree.put(“A”,43);
      initTree.put(“B”,32);
      System.out.println(“按键排序:”);
      System.out.println(initTree);
      List List=newarraylist(initTree.entrySet());
      Collections.sort(list,newcomparator(){
      @凌驾
      公共整数比较(Map.Entry e1,Map.Entry e2){
      返回e1.getValue().compareTo(e2.getValue());
      }
      });
      System.out.println(“按值排序:”);
      系统输出打印项次(列表);
      }
      }
      
      如果TreeMap只将键传递给Comparator,那么如果我在Comparator的构造函数中引用TreeMap,然后使用键获取值,这样做是否可行(不确定如何通过引用传递):class byValue实现Comparator{TreeMap theTree;public byValue(TreeMap theTree){this.theTree=theTree;}public int compare(String k1,String k2){//使用TreeMap的getKey方法来计算值}}@vito:no,因为通常这两个键中的一个还不在映射中,您将无法获得它的值。这不是在TreeMap的Comparator中执行此操作的示例吗?(尽管如此,如上所述,这确实违反了指定按键排序的
      SortedMap
      规范)@Marcus:that
      Comparator
      使用现有映射来获取要排序的值。换句话说,它不能对以后放入
      TreeMap的任意值进行排序,只能对原始映射中已经存在的值进行排序。如果添加map.put(“D”,2);结果仍然是“[C=1,B=2,A=3]”,而不是“[C=1,B=2,D=2,A=3]”修复:int res=e2.getValue().compareTo(e1.getValue());返回res!=0?res:1;新整数(0)==新整数(0)Yo
      
      static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
          SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
              new Comparator<Map.Entry<K,V>>() {
                  @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                      int res = e1.getValue().compareTo(e2.getValue());
                      if (e1.getKey().equals(e2.getKey())) {
                          return res; // Code will now handle equality properly
                      } else {
                          return res != 0 ? res : 1; // While still adding all entries
                      }
                  }
              }
          );
          sortedEntries.addAll(map.entrySet());
          return sortedEntries;
      }
      
      LinkedHashMap<Integer, String> sortedMap = 
          map.entrySet().stream().
          sorted(Entry.comparingByValue()).
          collect(Collectors.toMap(Entry::getKey, Entry::getValue,
                                   (e1, e2) -> e1, LinkedHashMap::new));
      
        TreeSet<Map.Entry<Integer, Double>> set = new TreeSet<>(new Comparator<Map.Entry<Integer, Double>>()
        {
          @Override
          public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2)
          {
            int valueComparison = o1.getValue().compareTo(o2.getValue());
            return valueComparison == 0 ? o1.getKey().compareTo(o2.getKey()) : valueComparison;
          }
        });
        int key = 5;
        double value = 1.0;
        set.add(new AbstractMap.SimpleEntry<>(key, value));
      
      import java.util.*;
      
      public class Main {
      
      public static void main(String[] args) {
          TreeMap<String, Integer> initTree = new TreeMap();
          initTree.put("D", 0);
          initTree.put("C", -3);
          initTree.put("A", 43);
          initTree.put("B", 32);
          System.out.println("Sorted by keys:");
          System.out.println(initTree);
          List list = new ArrayList(initTree.entrySet());
          Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
              @Override
              public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
                  return e1.getValue().compareTo(e2.getValue());
              }
          });
          System.out.println("Sorted by values:");
          System.out.println(list);
      }
      }