【问题标题】:Java sort map based on count of value of list基于列表值计数的Java排序映射
【发布时间】:2020-01-27 12:10:18
【问题描述】:

例如

Map<Home, List<People>> ihm = new TreeMap<Home, List<People>>();

数据如下:

ihm.put(new Home(...), Arrays.asList(new People(...),
new People(...),
new People(...));
ihm.put(new Home(...), Arrays.asList(new People(...),
new People(...));

我想按序号排序。住在房子里的人。

我如何使用比较器或可比较器来实现这一点?

【问题讨论】:

  • 这能回答你的问题吗? Sort a Map<Key, Value> by values
  • 来自 Javadocs for HashMap “这个类不保证地图的顺序;特别是,它不保证顺序会随着时间的推移保持不变。” - 如果你想订购它你不能使用HashMap
  • Hashmap 是一个无序集合,如果你想要一个有序的地图,那么使用SortedMap 并在地图中为你的值类型实现Comparable
  • 我已更改为 TreeMap/SortedMap 但在比较器/ble 上计算列表的值似乎仍然很复杂...
  • 为什么还需要地图?你可以有一个房子,它会有人与之相关联。

标签: java list sorting treemap


【解决方案1】:

不应将其作为地图键 Home 的属性来完成,因为您可能希望将人员添加/删除到 Home,从而破坏地图。

改为动态排序:

ihm.entrySet().stream()
   .sort(Comparator.comparingInt(es -> -es.getValue().size())) // Decreasing; neg. sizes.
   .forEach(es -> System.out.printf("...%n", ...));

【讨论】:

  • 你真的可以通过运行列表的大小来实现比较器吗?我认为比较器使用正或负回报来确定排序...
  • @Adam 对,我走捷径太多了。有(o1, o2) -&gt; ...Comparator.comparing o -&gt; property of o)风格。
【解决方案2】:

如何做到这一点的示例。

public static void main(String[] args){
    Map<Home, List<People>> ihm = new HashMap<Home, List<People>>();
    ihm.put(new Home(3), Arrays.asList(new People(1), new People(2),new People(3)));
    ihm.put(new Home(2), Arrays.asList(new People(1), new People(4)));
    ihm.put(new Home(4), Arrays.asList(new People(5), new People(4),new People(2),new People(3)));
    ihm.put(new Home(1), Collections.singletonList(new People(5)));

    System.out.println("\nUnSorted Map :");
    for (Map.Entry<Home, List<People>> entry:ihm.entrySet()) {
        System.out.println(entry.getKey());
    }

    Map<Home,List<People>> result = sortByValueCount(ihm);
    System.out.println("\nSorted Map :");
    for (Map.Entry<Home, List<People>> entry:result.entrySet()) {
        System.out.println(entry.getKey());
    }


}

public static Map<Home, List<People>> sortByValueCount(final Map<Home,List<People>> homeListMap) {
    return homeListMap.entrySet()
            .stream()
            .sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}

static class Home{
    int size;

    public Home(int size) {
        this.size = size;
    }



    @Override
    public String toString() {
        return "Home{" +
                "size=" + size +
                '}';
    }
}

static class People{
    int peopleNumber;

    public People(int peopleNumber) {
        this.peopleNumber = peopleNumber;
    }
}

为了更好地理解,我已将 Home 大小设置为与其中元素的大小相同。

【讨论】:

    【解决方案3】:

    你可以试试下面的代码吗?

    public class HomeMain {
    
    public static List<Map.Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){
    
        Set<Map.Entry<String, Integer>> set = wordMap.entrySet();
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(set);
        Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
        } );
        return list;
    }
    
    public static void main(String[] args) {
        Map<Home, List<People>> ihm = new HashMap<Home, List<People>>();
        ihm.put(new Home("Home"), Arrays.asList(new People(4),
                new People(5),
                new People(6)));
    
        ihm.put(new Home("Home1"), Arrays.asList(new People(2),
                new People(1),
                new People(9)));
    
        ihm.put(new Home("Home2"), Arrays.asList(new People(3),
                new People(6),
                new People(2)));
    
        ihm.put(new Home("Home3"), Arrays.asList(new People(1),
                new People(7),
                new People(6)));
        Map<String, Integer> newMap = new HashMap<String, Integer>();
    
        Iterator<Map.Entry<Home, List<People>>> itr = ihm.entrySet().iterator();
    
        while (itr.hasNext()) {
            Map.Entry<Home, List<People>> entry = itr.next();
            List<People> p = entry.getValue();
            int totalPeople = 0;
            for (People people : p) {
                totalPeople += people.getNumberOfPeople();
            }
            newMap.put(entry.getKey().getName(), totalPeople);
        }
    
    
        Iterator<Map.Entry<String, Integer>> it1 = newMap.entrySet().iterator();
        System.out.println("UnSorted map:");
        while (it1.hasNext()) {
            Map.Entry<String, Integer> entry = it1.next();
            System.out.println("Key = " + entry.getKey() +
                    ", Value = " + entry.getValue());
        }
        List<Map.Entry<String, Integer>> sortedList = sortByValue(newMap);
        System.out.println("Sorted map:");
        for (Map.Entry<String, Integer> entry : sortedList) {
            System.out.println(entry.getKey() + " ====" + entry.getValue());
        }
    }} 
    
    public class Home {
      String name;
    public Home(String homename){
        this.name=homename;
    }}
    
    public class People {
     public int getNumberOfPeople() {
        return numberOfPeople;
    }
    int numberOfPeople;
        public People(int numOfPeople){
            this.numberOfPeople=numOfPeople;
        }}
    

    【讨论】:

    • 输出:未排序映射:键 = Java.Home@6d6f6e28,值 = 15 键 = Java.Home@45ee12a7,值 = 11 键 = Java.Home@135fbaa4,值 = 12 键 = Java。 Home@330bedb4, Value = 14 排序后的映射:Java.Home@6d6f6e28 ====15 Java.Home@330bedb4 ====14 Java.Home@135fbaa4 ====12 Java.Home@45ee12a7 ====11您可以打印家庭名称而不是家庭对象。
    【解决方案4】:
    static <K,V extends Collection> Map<K,V> sortMap(Map<K,V> map){
        return map.entrySet().stream()
                .sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 2016-11-07
      • 2022-10-06
      • 1970-01-01
      • 2021-12-23
      • 2016-09-23
      相关资源
      最近更新 更多