desislava_topuzakova

Sorting map by value (list count)

Apr 1st, 2021
1,960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import static java.util.Map.Entry.comparingByKey;
  4. import static java.util.Map.Entry.comparingByValue;
  5. import static java.util.stream.Collectors.toMap;
  6.  
  7. public class demo {
  8. public static void main(String[] args) {
  9.  
  10. //Map with value list -> sorting by count of list elements
  11. Map<String, List<String>> map = new HashMap<>();
  12. map
  13. .entrySet()
  14. .stream()
  15. .sorted((a, b) -> b.getValue().size() - a.getValue().size())
  16. .forEach(pair -> {
  17. System.out.println(pair.getKey());
  18. pair.getValue().forEach(el -> System.out.println(el));
  19. });
  20.  
  21.  
  22.  
  23.  
  24.  
  25. }
  26.  
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment