Sorting a HashMap according to keys in Java

Hello Everyone,

We are given the details of marks scored by students in form of a HashMap, where the name of the student is the Key and the marks scored is the Value. Our task is to sort the map according to the key values i.e the names of the students in the alphabetical(lexicographical) order.
Examples:

Input : Key = Jayant, Value = 80
Key = Anushka, Value = 80
Key = Amit, Value = 75
Key = Abhishek, Value = 90
Key = Danish, Value = 40
Output : Sorted Map according to Names:
Key = Abhishek, Value = 90
Key = Amit, Value = 75
Key = Anushka, Value = 80
Key = Danish, Value = 40
Key = Jayant, Value = 80

// Java Code to sort Map by key value

import java.util.*;

class sortmapKey {

// This map stores unsorted values

static Map<String, Integer> map = new HashMap<>();

// Function to sort map by Key

public static void sortbykey()

{

// TreeMap to store values of HashMap

TreeMap<String, Integer> sorted = new TreeMap<>();

// Copy all data from hashMap into TreeMap

sorted.putAll(map);

// Display the TreeMap which is naturally sorted

for (Map.Entry<String, Integer> entry : sorted.entrySet())

System.out.println( "Key = " + entry.getKey() +

", Value = " + entry.getValue());

}

// Driver Code

public static void main(String args[])

{

// putting values in the Map

map.put( "Jayant" , 80 );

map.put( "Abhishek" , 90 );

map.put( "Anushka" , 80 );

map.put( "Amit" , 75 );

map.put( "Danish" , 40 );

// Calling the function to sortbyKey

sortbykey();

}

}

Output

Key = Abhishek, Value = 90 Key = Amit, Value = 75 Key = Anushka, Value = 80 Key = Danish, Value = 40 Key = Jayant, Value = 80

Note: The TreeMap provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

Using TreeMap (Constructor)

  • Java

// Java Code to sort Map by key value

import java.util.*;

class sortmapKey {

// This map stores unsorted values

static Map<String, Integer> map = new HashMap<>();

// Function to sort map by Key

public static void sortbykey()

{

// TreeMap to store values of HashMap

TreeMap<String, Integer> sorted

= new TreeMap<>(map);

// Display the TreeMap which is naturally sorted

for (Map.Entry<String, Integer> entry :

sorted.entrySet())

System.out.println( "Key = " + entry.getKey()

+ ", Value = "

+ entry.getValue());

}

// Driver Code

public static void main(String args[])

{

// putting values in the Map

map.put( "Jayant" , 80 );

map.put( "Abhishek" , 90 );

map.put( "Anushka" , 80 );

map.put( "Amit" , 75 );

map.put( "Danish" , 40 );

// Calling the function to sortbyKey

sortbykey();

}

}

Output

Key = Abhishek, Value = 90 Key = Amit, Value = 75 Key = Anushka, Value = 80 Key = Danish, Value = 40 Key = Jayant, Value = 80

Using Java 8 Lambdas

Below is the implementation of the above approach:

  • Java

// Java Code to sort Map by key value

import java.util.*;

class sortmapKey {

// This map stores unsorted key

static Map<String, Integer> map = new HashMap<>();

// function to sort hashmap by keys

public static Map<String, Integer>

sortByKey(Map<String, Integer> hm)

{

// Create a list from elements of HashMap

List<Map.Entry<String, Integer> > list

= new LinkedList<Map.Entry<String, Integer> >(

hm.entrySet());

// Sort the list using lambda expression

Collections.sort(

list,

(i1, i2) -> i1.getKey().compareTo(i2.getKey()));

// put data from sorted list to hashmap

HashMap<String, Integer> temp

= new LinkedHashMap<String, Integer>();

for (Map.Entry<String, Integer> aa : list) {

temp.put(aa.getKey(), aa.getValue());

}

return temp;

}

// Driver Code

public static void main(String args[])

{

// putting values in the Map

map.put( "Jayant" , 80 );

map.put( "Abhishek" , 90 );

map.put( "Anushka" , 80 );

map.put( "Amit" , 75 );

map.put( "Danish" , 40 );

// Calling the function to sortbyKey

Map<String, Integer> hm1 = sortByKey(map);

// print the sorted hashmap

for (Map.Entry<String, Integer> en :

hm1.entrySet()) {

System.out.println( "Key = " + en.getKey()

+ ", Value = "

+ en.getValue());

}

}

}

Output

Key = Abhishek, Value = 90 Key = Amit, Value = 75 Key = Anushka, Value = 80 Key = Danish, Value = 40 Key = Jayant, Value = 80