How to sort a map in Java 8

Sort map in Java 8
Prior to Java 8, a Map could be sorted as explained in this article :
How to sort a Map in Java

The below examples demonstrate how we can sort a map in Java 8 using lambda expression and streams. These examples use a Map where country name is key and the name of its capital is the corresponding value.
 

Sort a Map by key in Java 8

This example sorts the countryCapitalMap in ascending order of keys(country names).

package com.topjavatutorial;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

public class MapSortByKeyExample {

  public static void main(String[] args) {
    Map<String, String> countryCapitalMap = new HashMap<String, String>();
    countryCapitalMap.put("guyana", "georgetown");
    countryCapitalMap.put("nepal", "kathmandu");
    countryCapitalMap.put("australia", "canberra");
    countryCapitalMap.put("india", "new delhi");
    countryCapitalMap.put("japan", "tokyo");

    System.out.println("Original Map : \n" + countryCapitalMap);

    Map<String, String> sortedMap = new LinkedHashMap<String, String>();

    Stream<Map.Entry<String, String>> stream = countryCapitalMap.entrySet()
        .stream();
    stream.sorted(Map.Entry.comparingByKey()).forEachOrdered(
        c -> sortedMap.put(c.getKey(), c.getValue()));

    System.out.println("Map sorted by key : \n" + sortedMap);
  }

}


 

Output:

Original Map :
{guyana=georgetown, japan=tokyo, nepal=kathmandu, australia=canberra, india=new delhi}

Map sorted by key :
{australia=canberra, guyana=georgetown, india=new delhi, japan=tokyo, nepal=kathmandu}

 

Sort a Map by value in Java 8

This example sorts the countryCapitalMap in ascending order of values(country capital names).

package com.topjavatutorial;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

public class MapSortByValueExample {

  public static void main(String[] args) {
    Map<String, String> countryCapitalMap = new HashMap<String, String>();
    countryCapitalMap.put("guyana", "georgetown");
    countryCapitalMap.put("nepal", "kathmandu");
    countryCapitalMap.put("australia", "canberra");
    countryCapitalMap.put("india", "new delhi");
    countryCapitalMap.put("japan", "tokyo");

    System.out.println("Original Map : \n" + countryCapitalMap);

    Map<String, String> sortedMap = new LinkedHashMap<String, String>();

    Stream<Map.Entry<String, String>> stream = countryCapitalMap.entrySet()
        .stream();
    stream.sorted(Map.Entry.comparingByValue()).forEachOrdered(
        c -> sortedMap.put(c.getKey(), c.getValue()));

    System.out.println("Map sorted by value : \n" + sortedMap);
  }

}


 

Output:

Original Map :
{guyana=georgetown, japan=tokyo, nepal=kathmandu, australia=canberra, india=new delhi}

Map sorted by key :
{australia=canberra, guyana=georgetown, india=new delhi, japan=tokyo, nepal=kathmandu}

 

Sort a Map by key in reverse order

For sorting a map in reverse order, use Collections.reverseOrder() as shown below :

package com.topjavatutorial;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

public class MapReverseSortByKeyExample {

  public static void main(String[] args) {
    Map<String, String> countryCapitalMap = new HashMap<String, String>();
    countryCapitalMap.put("guyana", "georgetown");
    countryCapitalMap.put("nepal", "kathmandu");
    countryCapitalMap.put("australia", "canberra");
    countryCapitalMap.put("india", "new delhi");
    countryCapitalMap.put("japan", "tokyo");

    System.out.println("Original Map : \n" + countryCapitalMap);

    Map<String, String> sortedMap = new LinkedHashMap<String, String>();

    Stream<Map.Entry<String, String>> stream = countryCapitalMap.entrySet()
        .stream();
    stream.sorted(Collections.reverseOrder(Map.Entry.comparingByKey()))
        .forEachOrdered(c -> sortedMap.put(c.getKey(), c.getValue()));

    System.out.println("Map sorted by key in reverse order : \n" + sortedMap);
  }

}


 

Output:

Original Map :
{guyana=georgetown, japan=tokyo, nepal=kathmandu, australia=canberra, india=new delhi}

Map sorted by key in reverse order :
{nepal=kathmandu, japan=tokyo, india=new delhi, guyana=georgetown, australia=canberra}

 

Sort a Map by key in reverse order

Similar to sorting by keys in reverse order, we can use Collections.reverseOrder() to sort the map in reverse order of values.

package com.topjavatutorial;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

public class MapReverseSortByValueExample {

  public static void main(String[] args) {
    Map<String, String> countryCapitalMap = new HashMap<String, String>();
    countryCapitalMap.put("guyana", "georgetown");
    countryCapitalMap.put("nepal", "kathmandu");
    countryCapitalMap.put("australia", "canberra");
    countryCapitalMap.put("india", "new delhi");
    countryCapitalMap.put("japan", "tokyo");

    System.out.println("Original Map : \n" + countryCapitalMap);

    Map<String, String> sortedMap = new LinkedHashMap<String, String>();

    Stream<Map.Entry<String, String>> stream = countryCapitalMap.entrySet()
        .stream();
    stream.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
        .forEachOrdered(c -> sortedMap.put(c.getKey(), c.getValue()));

    System.out.println("Map sorted by value in reverse order : \n" + sortedMap);
  }

}


 

Output:

Original Map :
{guyana=georgetown, japan=tokyo, nepal=kathmandu, australia=canberra, india=new delhi}

Map sorted by value in reverse order :
{japan=tokyo, india=new delhi, nepal=kathmandu, guyana=georgetown, australia=canberra}
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

You may also like...

3 Responses

  1. October 10, 2017

    […] If you are using Java8, refer this article instead: Sort a Map in Java 8 […]

  2. January 30, 2018

    […] How to sort a map in Java 8   […]

  3. March 15, 2018

    […] How to sort a Map in Java 8 […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: