首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >TreeMap按值排序

TreeMap按值排序
EN

Stack Overflow用户
提问于 2010-05-19 18:56:35
回答 5查看 309K关注 0票数 160

我想写一个比较器,让我按值对TreeMap排序,而不是默认的自然排序。

我试过这样的东西,但找不到哪里出了问题:

代码语言:javascript
复制
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;
        }
    }
}

我想我要问的是:我可以将一个Map.Entry传递给比较器吗?

EN

回答 5

Stack Overflow用户

发布于 2013-10-30 08:45:52

在Java 8中:

代码语言:javascript
复制
LinkedHashMap<Integer, String> sortedMap = map.entrySet().stream()
  .sorted(Map.Entry.comparingByValue(/* Optional: Comparator.reverseOrder() */))
  .collect(Collectors.toMap(Map.Entry::getKey,
                            Map.Entry::getValue,
                            (e1, e2) -> e1, LinkedHashMap::new));
票数 61
EN

Stack Overflow用户

发布于 2014-07-11 06:58:14

我知道这篇文章特别要求按值对TreeMap进行排序,但对于那些并不真正关心实现但希望在添加元素时保持集合排序的解决方案的人来说,我希望得到这个基于TreeSet的解决方案的反馈。首先,元素不容易通过键检索,但对于我手头的用例(查找具有最低值的n个键),这不是必需的。

代码语言:javascript
复制
  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));
票数 3
EN

Stack Overflow用户

发布于 2013-01-21 03:22:32

很多人听人建议使用List,我也更喜欢使用它

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

代码语言:javascript
复制
    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;
        }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2864840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档