Java Map Sort sortByMap(Map map, int size)

Here you can find the source of sortByMap(Map map, int size)

Description

sort By Map

License

LGPL

Declaration

public static <T> List<T> sortByMap(Map<T, Double> map, int size) 

Method Source Code

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

Related

  1. sortByKey(Map map)
  2. sortByKey(T map, final boolean descending)
  3. sortByKeys( Map map)
  4. sortByKeys(Map map)
  5. sortByMap(List> lists)
  6. sortByValue( final Map map)
  7. sortByValue( Map map)
  8. sortByValue( Map map, final boolean ascendingValues)
  9. sortByValue(final Map m)