Java 8 HashMap<Integer, ArrayList>
Clash Royale CLAN TAG#URR8PPP
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.
I tried to look for a Java 8 way to sort Keys and also value.
HashMap> map
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.
groupingBy
Collector
Maybe, you could use a TreeMap instead of a HashMap. TreeMaps order by key as a feature of the implementation.
– PJ Fanning
Aug 11 at 23:12
2 Answers
2
To sort by key, you could use a 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
. Putting it all together:
TreeMap
Map.values().forEach()
List.sort
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.
just a little cleaner with reference, you can use
sortedByKey.values().forEach(Collections::sort);
for natural order sorting of the list– nullpointer
Aug 12 at 5:59
sortedByKey.values().forEach(Collections::sort);
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. Then sort the existing value List
before you pass it to the Collector
. Also notice that I have ignored the mergeFunction
since there can not be any collisions between values associated with the same key.
TreeMap
List
Collector
mergeFunction
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use a
groupingBy
with a downstreamCollector
.– Boris the Spider
Aug 11 at 22:04