This example shows how to sort LinkedHashMap by values in Java. This example also shows how to sort LinkedHashMap by values using a custom Comparator.
How to sort LinkedHashMap by value in Java?
How to get sorted values from the LinkedHashMap object?
If you want only sorted values from the LinkedHashMap, you can get all the values from the LinkedHashMap using the values
method, convert it to a List and then sort that List object as given below.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; public class SortLinkedHashMapByValuesExample { public static void main(String[] args) { LinkedHashMap<String, Integer> lhmap = new LinkedHashMap<String, Integer>(); lhmap.put("One", 1); lhmap.put("Six", 6); lhmap.put("Four", 4); lhmap.put("Seven", 7); lhmap.put("Two", 2); lhmap.put("Five", 5); lhmap.put("Three", 3); //get all the values from the LinkedHashMap List<Integer> list = new ArrayList<Integer>( lhmap.values() ); /* * Sort the value list using the * sort method of the Collections class */ Collections.sort(list); //print the sorted values List System.out.println("Sorted Values in ascending order"); for(Integer value : list) System.out.println(value); /* * If you want to sort values in descending order, * use the below given code. */ Collections.sort(list, new Comparator<Integer>(){ public int compare(Integer i1, Integer i2) { return i2.compareTo(i1); } }); //print the sorted values List System.out.println("\nSorted Values in descending order"); for(Integer value : list) System.out.println(value); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Sorted Values in ascending order 1 2 3 4 5 6 7 Sorted Values in descending order 7 6 5 4 3 2 1 |
The sort
method of the Collections class sorts the List elements according to their natural order, for the Integer type that is ascending order. That is why we need to provide the custom comparator object to order the List with LinkedHashMap values in descending order.
How to sort key-value mappings using the values of LinkedHashMap?
Using the above given code, we only get the sorted values of the map object. What if you want to get all key-value mappings or entries sorted by the values?
Well, in that case, we can get all the entries from the LinkedHashMap using the entrySet
method, convert it to a List and then sort the values List using a custom comparator as given below.
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 30 31 32 33 34 35 36 37 38 39 |
LinkedHashMap<String, Integer> lhmap = new LinkedHashMap<String, Integer>(); lhmap.put("One", 1); lhmap.put("Six", 6); lhmap.put("Four", 4); lhmap.put("Seven", 7); lhmap.put("Two", 2); lhmap.put("Five", 5); lhmap.put("Three", 3); //get all entries from the LinkedHashMap and convert it to a List List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>( lhmap.entrySet() ); Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>(){ public int compare(Entry<String, Integer> entry1, Entry<String, Integer> entry2) { return entry1.getValue() - entry2.getValue(); } }); System.out.println("LinkedHashMap sorted entries"); for(Map.Entry<String, Integer> entry : entries){ System.out.println( entry.getKey() + "=>" + entry.getValue() ); } /* * If you want sorted entries in the original LinkedHashMap, * use the below given code */ //clear the original map lhmap.clear(); //put all sorted entries back to the original object for(Map.Entry<String, Integer> entry : entries){ lhmap.put(entry.getKey(), entry.getValue()); } System.out.println("LinkedHashMap contains: " + lhmap); |
Output
1 2 3 4 5 6 7 8 9 |
LinkedHashMap sorted entries One=>1 Two=>2 Three=>3 Four=>4 Five=>5 Six=>6 Seven=>7 LinkedHashMap contains: {One=1, Two=2, Three=3, Four=4, Five=5, Six=6, Seven=7} |
For sorting the entries in descending order, change the sort
method as given below.
1 2 3 4 5 6 7 |
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>(){ public int compare(Entry<String, Integer> entry1, Entry<String, Integer> entry2) { return entry2.getValue() - entry1.getValue(); } }); |
Output
1 2 3 4 5 6 7 8 9 |
LinkedHashMap sorted entries Seven=>7 Six=>6 Five=>5 Four=>4 Three=>3 Two=>2 One=>1 LinkedHashMap contains: {Seven=7, Six=6, Five=5, Four=4, Three=3, Two=2, One=1} |
How to sort LinkedHashMap by values having custom class objects?
All the above examples used the objects of an Integer class as a value of the map that has implemented the Comparable interface.
If the LinkedHashMap values are objects of a custom class, that the custom class must implement the Comparable interface for the sort
method to work, or you must provide the custom comparator while using the sort
method.
Using the Comparable interface:
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 30 31 32 33 34 35 36 37 38 39 40 |
class Emp implements Comparable<Emp>{ private Integer id; public Emp(Integer id){ this.id = id; } public String toString(){ return "ID: " + this.id; } public Integer getId(){ return this.id; } public int compareTo(Emp otherEmp) { return this.getId() - otherEmp.getId(); } } public class SortLinkedHashMapByValuesExample { public static void main(String[] args) { LinkedHashMap<String, Emp> lhmap = new LinkedHashMap<String, Emp>(); lhmap.put("2", new Emp(2)); lhmap.put("3", new Emp(3)); lhmap.put("1", new Emp(1)); //get all the values from map List<Emp> list = new ArrayList<Emp>( lhmap.values() ); Collections.sort(list); System.out.println("Sorted Values in ascending order"); for(Emp emp : list) System.out.println(emp); } } |
Output
1 2 3 4 |
Sorted Values in ascending order ID: 1 ID: 2 ID: 3 |
Using the Comparator interface:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class Emp{ private Integer id; public Emp(Integer id){ this.id = id; } public String toString(){ return "ID: " + this.id; } public Integer getId(){ return this.id; } } public class SortLinkedHashMapByValuesExample { public static void main(String[] args) { LinkedHashMap<String, Emp> lhmap = new LinkedHashMap<String, Emp>(); lhmap.put("2", new Emp(2)); lhmap.put("3", new Emp(3)); lhmap.put("1", new Emp(1)); //get all the values from the map List<Emp> list = new ArrayList<Emp>( lhmap.values() ); Collections.sort(list, new Comparator<Emp>(){ public int compare(Emp emp1, Emp emp2) { return emp1.getId() - emp2.getId(); } }); System.out.println("Sorted Values in ascending order"); for(Emp emp : list) System.out.println(emp); } } |
Output
1 2 3 4 |
Sorted Values in ascending order ID: 1 ID: 2 ID: 3 |
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap