Here you can find the source of sortedView(Map
public static Map<String, Long> sortedView(Map<String, Long> original, int numItems)
//package com.java2s; //License from project: Open Source License import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class Main { public static Map<String, Long> sortedView(Map<String, Long> original, int numItems) { List<Map.Entry<String, Long>> values = new LinkedList<Map.Entry<String, Long>>(original.entrySet()); Collections.sort(values, new Comparator<Map.Entry<String, Long>>() { public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) { int i = o1.getValue().compareTo(o2.getValue()); return (-i); }// w w w . j a v a 2 s.c om }); Map<String, Long> view = new LinkedHashMap<String, Long>(); for (int i = 0; i < values.size() && i < numItems; i++) { Map.Entry<String, Long> entry = values.get(i); view.put(entry.getKey(), entry.getValue()); } return (view); } }