In this tutorial, we will see “How to sort a Map by Key & Value in Java 8 using LinkedHashMap?”

Approach/Steps:

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.

Sort a Map in Java 8 with examples...!!! Share on X

Example 1. Sort Map by Key using LinkedHashMap with Collect

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);

    }

}

Output:

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

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);

    }

}

Output:

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

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);

    }
}

Output:

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

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);

    }
}

Output:

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}

Sort a Map in Java 8 with examples...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts