Today I am going to explain you how we sort MAP by Key or by Value in Java. First of all you have to create a Map object and add some values in it so that we can do processing on it, see below how we create Map object and add values in it.
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 30);
map.put("c", 25);
map.put("b", 35);
map.put("e", 40);
map.put("f", 20);
map.put("g", 15);
map.put("d", 10);
Here the type of Key is String and type of Value is Integer, Now if we want to sort Map by Key then you can use TreeMap because TreeMap store object in sorted form.
Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
System.out.println("Sorted Map by Key");
for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
You May Like
List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(map.entrySet());
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
@Override
public int compare( Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2 )
{
return (obj1.getValue()).compareTo( obj2.getValue() );
}
} );
Iterator i = list.listIterator();
System.out.println("Sorted Map by Value");
while(i.hasNext()) {
Map.Entry mapentry = (Map.Entry)i.next();
System.out.println("Key : " + mapentry.getKey() + " Value : " + mapentry.getValue());
}
This code sort Map by Value in ascending order but if you want to sort Map by value in descending order then modify line return (obj1.getValue()).compareTo( obj2.getValue() ); by return (obj2.getValue()).compareTo( obj1.getValue() );
Below is the complete code:
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class SortMapByValue2 {
public static void main(String a[]){
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 30);
map.put("c", 25);
map.put("b", 35);
map.put("e", 40);
map.put("f", 20);
map.put("g", 15);
map.put("d", 10);
Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
System.out.println("Sorted Map by Key");
for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());
}
List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(map.entrySet());
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
@Override
public int compare( Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2 )
{
return (obj1.getValue()).compareTo( obj2.getValue() );
}
} );
Iterator i = list.listIterator();
System.out.println("Sorted Map by Value");
while(i.hasNext()) {
Map.Entry mapentry = (Map.Entry)i.next();
System.out.println("Key : " + mapentry.getKey() + " Value : " + mapentry.getValue());
}
}
}
Note: If you like this post or have any question then just drop a comment i will reply you as soon as possible.
0 Comments