gogoWebsite

Map sorted by Value in Java

Updated to 22 days ago
public static void valueUpSort() { // By default, TreeMap is sorted in ascending order of key Map<String, Integer> map = new TreeMap<String, Integer>(); ("acb1", 5); ("bac1", 3); ("bca1", 20); ("cab1", 80); ("cba1", 1); ("abc1", 10); ("abc2", 12); // Ascending comparator Comparator<<String, Integer>> valueComparator = new Comparator<<String,Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { // TODO Auto-generated method stub return ()-(); } }; // Convert map to list for sorting List<<String, Integer>> list = new ArrayList<<String,Integer>>(()); // Sort (list,valueComparator); // By default, TreeMap sorts the keys in ascending order ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- for (<String, Integer> entry : list) { (() + ":" + ()); } }