package SortedSet;
import java.util.*;
public class HashMapValueSort {
public static void main(String[] args){
final Map map = new HashMap();
map.put(4,"Mango");
map.put(3,"Apple");
map.put(5,"Orange");
map.put(8,"Fruits");
map.put(23,"Vegetables");
map.put(1,"Zebra");
map.put(5,"Yellow");
System.out.println(map);
final HashMapValueSort sort = new HashMapValueSort();
final Set> entry = map.entrySet();
final Comparator> comparator = new Comparator>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
String value1 = o1.getValue();
String value2 = o2.getValue();
return value1.compareTo(value2);
}
};
final SortedSet> sortedSet = new TreeSet(comparator);
sortedSet.addAll(entry);
final Map sortedMap = new LinkedHashMap();
for(Map.Entry entry1 : sortedSet ){
sortedMap.put(entry1.getKey(),entry1.getValue());
}
System.out.println(sortedMap);
}
}