Ascending order sorting a HashMap,TreeMap in Java
Here is the code
TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>();
Iterator<Entry<String, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Entry<String, Integer> entry = entries.next();
treeMap.put(entry.getKey(), entry.getValue());
}
SortedSet<Map.Entry<String,Integer>> sm = new TreeSet<Map.Entry<String ,Integer>>();
sm = treeMap; // This is sorted map in ascending order
Happy coding
Comments
Post a Comment