Here you can find the source of sortByMap(Map
public static <T> List<T> sortByMap(Map<T, Double> map, int size)
//package com.java2s; //License from project: LGPL import java.util.ArrayList; import java.util.List; import java.util.Map; public class Main { public static <T> List<T> sortByMap(Map<T, Double> map, int size) { double worstTopScore = Double.MAX_VALUE; T resultWorstValue = null;/*from w ww.ja v a 2s . com*/ List<T> result = new ArrayList<T>(size); for (T k : map.keySet()) { double score = map.get(k); if (result.size() < size) { result.add(k); if (score < worstTopScore) { worstTopScore = score; resultWorstValue = k; } } else { if (score > worstTopScore) { result.remove(resultWorstValue); result.add(k); worstTopScore = score; resultWorstValue = k; } } } return result; } }