Java Collection How to - Get top 10 values in hash map








Question

We would like to know how to get top 10 values in hash map.

Answer

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/*w w  w.  ja  va2 s.  c  om*/
public class Main {

    public static void main(String[] args) {

        HashMap<String,Double> map = new HashMap<String,Double>();
        ValueComparator bvc =  new ValueComparator(map);
        TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);

        map.put("A",9.0);
        map.put("B",6.0);
        map.put("C",7.0);
        map.put("D",10.0);

        System.out.println("unsorted map: "+map);

        sorted_map.putAll(map);

        System.out.println("results: "+sorted_map);
    }
}

class ValueComparator implements Comparator<String> {

    Map<String, Double> base;
    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }
  
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        }
    }
}

The code above generates the following result.