Java tutorial
//package com.java2s; import java.util.*; public class Main { public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> sortByValues(Map<K, V> map) { SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<>((e1, e2) -> { int res = e1.getValue().compareTo(e2.getValue()); return res != 0 ? res : 1; }); sortedEntries.addAll(map.entrySet()); return sortedEntries; } }