### Question:
MapUtils { public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue( Map<K, V> map) { return sortByValue(map, false); }  static K keyOfHighestValue(Map<K, V> map); static LinkedHashMap<K, V> keysOfHighestValues(
            Map<K, V> map, int topN); static LinkedHashMap<K, V> keysOfHighestValues(
            Map<K, V> map, int topN, final boolean reverse); static Map<K, V> sortByValue(
            Map<K, V> map); static Map<K, V> sortByValue(
            Map<K, V> map, final boolean reverse);  }### Answer:
@Test public void testSortByValue() { Random random = new Random(System.currentTimeMillis()); Map<String, Integer> testMap = new HashMap<String, Integer>(1000); for (int i = 0; i < 1000; ++i) { testMap.put("SomeString" + random.nextInt(), random.nextInt()); } testMap = MapUtils.sortByValue(testMap); assertEquals(1000, testMap.size()); Integer previous = null; for (Map.Entry<String, Integer> entry : testMap.entrySet()) { assertNotNull(entry.getValue()); if (previous != null) { assertTrue(entry.getValue() >= previous); } previous = entry.getValue(); } }
@Test public void testSortByReverseValue() { Random random = new Random(System.currentTimeMillis()); Map<String, Integer> testMap = new HashMap<String, Integer>(1000); for (int i = 0; i < 1000; ++i) { testMap.put("SomeString" + random.nextInt(), random.nextInt()); } testMap = MapUtils.sortByValue(testMap, true); assertEquals(1000, testMap.size()); Integer previous = null; for (Map.Entry<String, Integer> entry : testMap.entrySet()) { assertNotNull(entry.getValue()); if (previous != null) { assertTrue(entry.getValue() <= previous); } previous = entry.getValue(); } }