简体   繁体   English

Java 8 HashMap <Integer,ArrayList <Integer >>

[英]Java 8 HashMap<Integer, ArrayList<Integer>>

I am new Java 8 and want to sort a Map based on Key and then sort each list within values. 我是新的Java 8,想要基于Key对Map进行排序,然后对值中的每个列表进行排序。

I tried to look for a Java 8 way to sort Keys and also value. 我试图寻找一种Java 8方式来对Keys进行排序,也很有价值。 HashMap> map HashMap>地图

map.entrySet().stream().sorted(Map.Entry.comparingByKey())
    .collect(Collectors.toMap(Map.Entry::getKey, 
        Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));

I am able to sort the Map and I can collect each values within map to sort but is their a way we can do in java 8 and can both be combined. 我能够对Map进行排序,我可以收集map中的每个值进行排序,但这是我们在java 8中可以做到的一种方式,并且可以将它们组合在一起。

To sort by key, you could use a TreeMap . 要按键排序,可以使用TreeMap To sort each list in the values, you could iterate over the values of the map by using the Map.values().forEach() methods and then sort each list by using List.sort . 要对值中的每个列表进行排序,可以使用Map.values().forEach()方法迭代映射的值,然后使用List.sort对每个列表进行List.sort Putting it all together: 把它们放在一起:

Map<Integer, List<Integer>> sortedByKey = new TreeMap<>(yourMap);

sortedByKey.values().forEach(list -> list.sort(null)); // null: natural order

This sorts each list in-place , meaning that the original lists are mutated. 这会对每个列表进行就地排序,这意味着原始列表会发生变异。


If instead you want to create not only a new map, but also new lists for each value, you could do it as follows: 相反,如果您不仅要创建新地图,还要为每个值创建新列表,则可以按如下方式创建:

Map<Integer, List<Integer>> sortedByKey = new TreeMap<>(yourMap);

sortedByKey.replaceAll((k, originalList) -> { 
    List<Integer> newList = new ArrayList<>(originalList);
    newList.sort(null); // null: natural order
    return newList;
});

EDIT: 编辑:

As suggested in the comments, you might want to change: 正如评论中所建议的那样,您可能想要更改:

sortedByKey.values().forEach(list -> list.sort(null));

By either: 通过:

sortedByKey.values().forEach(Collections::sort);

Or: 要么:

sortedByKey.values().forEach(list -> list.sort(Comparator.naturalOrder()));

Either one of the two options above is much more expressive and shows the developer's intention in a better way than using null as the comparator argument to the List.sort method. 上面两个选项中的任何一个都更具表现力,并且比使用null作为List.sort方法的比较器参数更好地显示开发人员的意图。

Same considerations apply for the approach in which the lists are not modified in-place. 同样的考虑因素适用于未就地修改列表的方法。

You can do it like so, 你可以这样做,

Map<Integer, List<Integer>> sortedMap = sourceMap.entrySet().stream()
    .collect(Collectors.toMap(Map.Entry::getKey,
        e -> e.getValue().stream().sorted().collect(Collectors.toList()), 
        (v1, v2) -> v1,
        TreeMap::new));

Notice the use of TreeMap which is sorted according to the natural ordering of its keys. 注意TreeMap的使用,它根据其键的自然顺序排序。 Then sort the existing value List before you pass it to the Collector . 然后在将现有值List传递给Collector之前对其进行排序。 Also notice that I have ignored the mergeFunction since there can not be any collisions between values associated with the same key. 另请注意,我忽略了mergeFunction因为与同一个键关联的值之间不会发生任何冲突。

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

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