The document discusses several ways to sort a HashMap by its keys or values in Java 8 and pre-Java 8. It explains how to sort in natural, reverse, and customized orders using streams, Entry.comparingByKey(), Entry.comparingByValue(), and a TreeMap. Methods include using Comparator.reverseOrder(), returning different comparisons in the comparator, and collecting the stream into a LinkedHashMap.
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0 ratings0% found this document useful (0 votes)
38 views5 pages
Java 8 Sort HashMap
The document discusses several ways to sort a HashMap by its keys or values in Java 8 and pre-Java 8. It explains how to sort in natural, reverse, and customized orders using streams, Entry.comparingByKey(), Entry.comparingByValue(), and a TreeMap. Methods include using Comparator.reverseOrder(), returning different comparisons in the comparator, and collecting the stream into a LinkedHashMap.
Java 8 sort HashMap by keys using customized Comparator : Map<String, Integer> sortedStudentMap = studentMap.entrySet() .stream() .sorted(Entry.comparingByKey((o1, o2) -> o1.length() - o2.length())) .collect(Collectors.toMap(Entry::g etKey, Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new)); To sort the same HashMap in decreasing order of length of keys, use either Collections.reverseOrder() or else return o2.length() - o1.length() instead of o1.length() - o2.length() as in the below program. Map<String, Integer> sortedStudentMap = studentMap.entrySet() .stream() .sorted(Entry.comparingByKey((o1, o2) -> o2.length() - o1.length()))
// OR // .sorted(Collections.reverseOrder(Entry.comparingByKey((o1, o2) -> o1.length() - o2.length())))
(Before Java 8)? inserting HashMap elements into TreeMap. It will sort the elements according to supplied Comparator or in natural order if no Comparator is supplied.
Sorting HashMap according to natural order of keys
using TreeMap without Comparator : Map<String, Integer> sortedStudentMap = new TreeMap<>(studentMap); Sorting HashMap in natural reverse order of keys using TreeMap with Comparator : Map<String, Integer> sortedStudentMap = new TreeMap<>(Collections.reverseOrder());
sortedStudentMap.putAll(studentMap); Sorting HashMap by keys using TreeMap with customized Comparator : Map<String, Integer> sortedStudentMap = new TreeMap<>(new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.length() - o2.length(); } });
sortedStudentMap.putAll(studentMap); To sort the same HashMap in decreasing order of length of keys, use either Collections.reverseOrder() or else return o2.length() - o1.length() instead of o1.length() - o2.length() as in the below program Map<String, Integer> sortedStudentMap = new TreeMap<>(new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.length() - o1.length(); } });
// OR
// Map<String, Integer> sortedStudentMap = new TreeMap<>(Collections.reverseOrder(new Comparator<String>() // { // @Override // public int compare(String o1, String o2) // { // return o1.length() - o2.length(); // } // }));