In this tutorial, we will see “How to sort a Map by Key & Value in Java 8 using LinkedHashMap?”
Approach/Steps:
1 2 3 4 5 6 7 | 1. Create a Map object and add values to it. 2. Convert Map object into a Stream. 3. Sort it now. 4. Finally, either collect it with LinkedHashMap OR perform the forEachOrdered operation with LinkedHashMap. |
Example 1. Sort Map by Key using LinkedHashMap with Collect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; public class Sort_Map_ByKey_With_Collect_Java8 { public static void main(String[] argv) { Map <String, Integer> unsortedMapObj = new HashMap <> (); unsortedMapObj.put("Tom", 50); unsortedMapObj.put("Brett", 45); unsortedMapObj.put("Ashton", 9); unsortedMapObj.put("Claire", 78); unsortedMapObj.put("Daniella", 2); System.out.println("Original Unsorted Map: \n" + unsortedMapObj); Map <String, Integer> sortedMapObj = unsortedMapObj .entrySet() .stream() .sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); System.out.println("\nResulted Sorted Map by Key in ascending order: \n" + sortedMapObj); } } |
1 2 3 4 5 | Original Unsorted Map: {Claire=78, Brett=45, Tom=50, Daniella=2, Ashton=9} Resulted Sorted Map by Key in ascending order: {Ashton=9, Brett=45, Claire=78, Daniella=2, Tom=50} |
Example 2. Sort Map by Key using LinkedHashMap with ForEachOrdered
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; public class Sort_Map_ByKey_With_ForEachOrdered_Java8 { public static void main(String[] args) { Map <String, Integer> unsortedMapObj = new HashMap <> (); unsortedMapObj.put("Tom", 50); unsortedMapObj.put("Brett", 45); unsortedMapObj.put("Ashton", 9); unsortedMapObj.put("Claire", 78); unsortedMapObj.put("Daniella", 2); System.out.println("Original Unsorted Map: \n" + unsortedMapObj); Map <String, Integer> sortedMapObj = new LinkedHashMap <> (); unsortedMapObj.entrySet() .stream() .sorted(Map.Entry.comparingByKey()) .forEachOrdered(i -> sortedMapObj.put(i.getKey(), i.getValue())); System.out.println("\nResulted Sorted Map by Key in ascending order: \n" + sortedMapObj); } } |
1 2 3 4 5 | Original Unsorted Map: {Claire=78, Brett=45, Tom=50, Daniella=2, Ashton=9} Resulted Sorted Map by Key in ascending order: {Ashton=9, Brett=45, Claire=78, Daniella=2, Tom=50} |
Example 3. Sort Map by Value using LinkedHashMap with Collect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; public class Sort_Map_ByValue_With_Collect_Java8 { public static void main(String[] argv) { Map <String, Integer> unsortedMapObj = new HashMap <> (); unsortedMapObj.put("Tom", 50); unsortedMapObj.put("Brett", 45); unsortedMapObj.put("Ashton", 9); unsortedMapObj.put("Claire", 78); unsortedMapObj.put("Daniella", 2); System.out.println("Original Unsorted Map: \n" + unsortedMapObj); Map <String, Integer> sortedMapObj = unsortedMapObj.entrySet() .stream() .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); System.out.println("\nResulted Sorted Map by value in ascending order: \n" + sortedMapObj); } } |
1 2 3 4 5 | Original Unsorted Map: {Claire=78, Brett=45, Tom=50, Daniella=2, Ashton=9} Resulted Sorted Map by value in ascending order: {Daniella=2, Ashton=9, Brett=45, Tom=50, Claire=78} |
Example 4. Sort Map by Value using LinkedHashMap with ForEachOrdered
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; public class Sort_Map_ByValue_With_ForEachOrdered_Java8 { public static void main(String[] argv) { Map <String, Integer> unsortedMapObj = new HashMap <> (); unsortedMapObj.put("Tom", 50); unsortedMapObj.put("Brett", 45); unsortedMapObj.put("Ashton", 9); unsortedMapObj.put("Claire", 78); unsortedMapObj.put("Daniella", 2); System.out.println("Original Unsorted Map: \n" + unsortedMapObj); Map <String, Integer> sortedMapObj = new LinkedHashMap <> (); unsortedMapObj.entrySet() .stream() .sorted(Map.Entry. <String, Integer> comparingByValue()) .forEachOrdered(x -> sortedMapObj.put(x.getKey(), x.getValue())); System.out.println("\nResulted Sorted Map by value in ascending order: \n" + sortedMapObj); } } |
1 2 3 4 5 | Original Unsorted Map: {Claire=78, Brett=45, Tom=50, Daniella=2, Ashton=9} Resulted Sorted Map by value in ascending order: {Daniella=2, Ashton=9, Brett=45, Tom=50, Claire=78} |
Do you like this Post? – then check my other helpful posts: