Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
Map< Integer,Integer> map=new HashMap<>(); Comparator<Map.Entry<Integer, Integer>> valueComparator=new Comparator<Map.Entry<Integer,Integer>>() { @Override public int compare(Entry<Integer, Integer> arg0, Entry<Integer, Integer> arg1) { return arg1.compareTo(arg0); } }; List<Map.Entry<Integer, Integer>> list=new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet()); Collections.sort(list,valueComparator); for(Map.Entry<Integer, Integer> entry:list) { System.out.println(entry.getKey()+":"+entry.getValue()); }
Map< Integer,Integer> map=new HashMap<>(); Comparator<Map.Entry<Integer, Integer>> valueComparator=new Comparator<Map.Entry<Integer,Integer>>() { @Override public int compare(Entry<Integer, Integer> arg0, Entry<Integer, Integer> arg1) { return arg0.getValue()-arg1.getValue(); } }; List<Map.Entry<Integer, Integer>> list=new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet()); Collections.sort(list,valueComparator); for(Map.Entry<Integer, Integer> entry:list) { System.out.println(entry.getKey()+":"+entry.getValue()); }
Powered by codetiler.com