Java Map Sort sortedView(Map original, int numItems)

Here you can find the source of sortedView(Map original, int numItems)

Description

sorted View

License

Open Source License

Declaration

public static Map<String, Long> sortedView(Map<String, Long> original, int numItems) 

Method Source Code

//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);
    }
}

Related

  1. sortedKeys(Map map)
  2. sortedMap(Map map, Comparator comparator)
  3. sortedString(Map c)
  4. sortedString(Map c)
  5. sortedTable(Map map)
  6. sortEntries(Map map, Comparator> comparator)
  7. sortHashMapByValuesD(HashMap passedMap)
  8. sortingHelperSessions(String column, Map sortParams)
  9. sortKeysByValue(final Map m)