Sorting the outer map Map<String, Map<String, List<Integer>>>
by the list size in the nested map retaining the outer and inner keys as before.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to toggle on Order in ReactJS
- StackExchange API - Deserialize Date in JSON Respo
You may solve this by generalizing the process:
This method creates a new map with the same keys as the specified one, replacing all values using the specified function and sorting the entries according to the reversal of the specified comparator. It uses Java 9’s
Map.entry(…, …)
factory. If you have to support Java 8 ornull
keys or values, you may usenew AbstractMap.SimpleImmutableEntry<>(…, …)
instead.This method can now be used to replace you inner map’s
List
s withInteger
s representing their sizes and sort them in descending order and use the replacement operation as replacement function of the outer map:This does basically the same as your posted solution. The outer map’s comparator uses the fact that the new inner maps are already sorted, so their first value is the maximum. But there must be no empty inner map.
This could easily get adapted to keep the
List<ThreadDo>
and just sort them by size:We only have to change the inner map’s replacement function to
Function.identity()
and provide a comparator using the list’s sizes. The outer map’s comparator still can use the fact that the inner maps are already sorted at this point, but also has to extract thesize()
of the list for comparison.