Sunday 24 August 2014

Sort HashMap by key and value

HashMap don't return the data in order, since it stores data based on hash function. It is always good to know how HashMap works in Java, Contract between HashCode and equals.

Sort the HashMap data by using key
It is very simple, pass the HashMap object to TreeMap constructor.

import java.util.*;

public class SortMapByKey {
    public static void main(String args[]){
        Map<Integer, String> map  = new HashMap<> ();
        
        map.put(5, "Krishna");
        map.put(11, "Arjun");
        map.put(4, "Joel");
        map.put(77, "ptr");
        map.put(99, "Murali");
        
        System.out.println("Before sorting");
        Set<Integer> keys = map.keySet();
        Iterator<Integer> iter = keys.iterator();
        
        while(iter.hasNext()){
            Integer key = iter.next();
            System.out.println(key +" " + map.get(key));
        }
        
        System.out.println("After Sorting");
        Map<Integer, String> sortMap = new TreeMap(map);
        
        keys = sortMap.keySet();
        iter = keys.iterator();
        
        while(iter.hasNext()){
            Integer key = iter.next();
            System.out.println(key +" " + map.get(key));
        }
        
    }
}


Output
Before sorting
99 Murali
4 Joel
5 Krishna
11 Arjun
77 ptr
After Sorting
4 Joel
5 Krishna
11 Arjun
77 ptr
99 Murali

Sort HashMap data by using value
import java.util.*;

public class SortMapByValue {
    
    static Map sortMap(Map map){
        List list = new LinkedList(map.entrySet());
        
       Collections.sort(list, new Comparator() {
            @Override
            public int compare(Object obj1, Object obj2) {
               return ((Comparable) ((Map.Entry) (obj1)).getValue())
                  .compareTo(((Map.Entry) (obj2)).getValue());
            }
       });

       HashMap sortedHashMap = new LinkedHashMap();
       for (Iterator it = list.iterator(); it.hasNext();) {
              Map.Entry entry = (Map.Entry) it.next();
              sortedHashMap.put(entry.getKey(), entry.getValue());
       } 
       
       return sortedHashMap;
    }
    
     public static void main(String args[]){
        Map<Integer, String> map  = new HashMap<> ();
        
        map.put(5, "Krishna");
        map.put(11, "Arjun");
        map.put(4, "Joel");
        map.put(77, "ptr");
        map.put(99, "Murali");
        
        System.out.println("Before sorting");
        Set<Integer> keys = map.keySet();
        Iterator<Integer> iter = keys.iterator();
        
        while(iter.hasNext()){
            Integer key = iter.next();
            System.out.println(key +" " + map.get(key));
        }
        
        System.out.println("After Sorting");
        Map<Integer, String> sortMap = sortMap(map);
        
        keys = sortMap.keySet();
        iter = keys.iterator();
        
        while(iter.hasNext()){
            Integer key = iter.next();
            System.out.println(key +" " + map.get(key));
        }
    }
}


Output
Before sorting
99 Murali
4 Joel
5 Krishna
11 Arjun
77 ptr
After Sorting
11 Arjun
4 Joel
5 Krishna
99 Murali
77 ptr
 


                                                             Home

No comments:

Post a Comment