SringSring

Wednesday, 29 October 2014

Display OrderByValue in map

package Com.Srini.program;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class OrderByValue {
public static void main(String a[]){
       Map<String, Integer> map = new HashMap<String, Integer>();
       map.put("java", 20);
       map.put("C++", 45);
       map.put("Java2Novice", 2);
       map.put("Unix", 67);
       map.put("MAC", 26);
       map.put("Why this kolavari", 93);
       Set<Entry<String, Integer>> set = map.entrySet();
       List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
       Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
       {
           public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
           {
               return (o2.getValue()).compareTo( o1.getValue() );
           }
       } );
       for(Map.Entry<String, Integer> entry:list){
           System.out.println(entry.getKey()+" ==== "+entry.getValue());
       }
/*Map<String, Integer> map = new HashMap<String, Integer>();
       map.put("java", 20);
       map.put("C++", 45);
       map.put("Java2Novice", 2);
       map.put("Unix", 67);
       map.put("MAC", 26);
       map.put("Why this kolavari", 93);
       Set<Entry<String, Integer>> set = map.entrySet();
     
       Iterator itr = set.iterator();
       while(itr.hasNext())
       {
           System.out.println(itr.next());
       }*/
     
   }
}

No comments:

Post a Comment