Java Map Sort sortMap(Map map, int k)

Here you can find the source of sortMap(Map map, int k)

Description

sort Map

License

LGPL

Declaration

public static List<Map.Entry<Integer, Double>> sortMap(Map<Integer, Double> map, int k) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import java.util.List;
import java.util.Map;

public class Main {
    public static List<Map.Entry<Integer, Double>> sortMap(Map<Integer, Double> map, int k) {
        //System.out.println("@@@@@@@@@@@@@"+map);
        List<Map.Entry<Integer, Double>> list = new ArrayList<Map.Entry<Integer, Double>>(map.entrySet());

        Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() {
            @Override//from w w  w.  j  a va  2 s .  com
            public int compare(Map.Entry<Integer, Double> arg0, Map.Entry<Integer, Double> arg1) {
                return (arg1.getValue().compareTo(arg0.getValue()));
            }
        });

        if (k > list.size())
            k = list.size();

        List<Map.Entry<Integer, Double>> result = new ArrayList<Map.Entry<Integer, Double>>(k);

        for (int i = 0; i < k; i++) {
            result.add(list.get(i));
        }

        return result;
    }
}

Related

  1. sortKeyValuePairByValue( Map map)
  2. sortMap(final Map map, final Comparator> comparator)
  3. sortMap(LinkedHashMap map, Comparator> c)
  4. sortMap(Map oldMap)
  5. sortMap(Map oldMap, final boolean asc)
  6. sortMap(Map map, Comparator> compator)
  7. sortMapByKey(Map data)
  8. sortMapByKey(Map map)
  9. sortMapByValue(Map input, final boolean desc)