Here you can find the source of sortMapByValue(Map
public static List<Map.Entry<String, Integer>> sortMapByValue(Map<String, Integer> oriMap)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static List<Map.Entry<String, Integer>> sortMapByValue(Map<String, Integer> oriMap) { if (oriMap == null || oriMap.isEmpty()) { return null; }//from w ww. j ava 2 s. c om List<Map.Entry<String, Integer>> entries = new ArrayList<>(oriMap.entrySet()); Collections.sort(entries, (o1, o2) -> o2.getValue() - o1.getValue()); return entries; } }