Mapmap = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (Map.Entry entry : map.entrySet()) { System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); }
MapThis code sorts the map by value and prints the result to the console. Library: java.util and java.util.Collectionsmap = new HashMap<>(); map.put("A", 50); map.put("B", 20); map.put("C", 30); List > list = new LinkedList<>(map.entrySet()); Collections.sort(list, new Comparator >() { public int compare(Map.Entry o1, Map.Entry o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); for (Map.Entry entry : list) { System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); }