Here you can find the source of sortByComparatorDouble( final Map
Parameter | Description |
---|---|
map | : HashMap<String, Double> |
public static Map<String, Double> sortByComparatorDouble( final Map<String, Double> map)
//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; import java.util.Map.Entry; public class Main { /**//w ww . j a va 2 s . c om * Descending order HashMap by value * @param map : HashMap<String, Double> */ public static Map<String, Double> sortByComparatorDouble( final Map<String, Double> map) { List<Entry<String, Double>> map_arr = new LinkedList<Entry<String, Double>>( map.entrySet()); Collections.sort(map_arr, new Comparator<Entry<String, Double>>() { public int compare(Entry<String, Double> v1, Entry<String, Double> v2) { return v2.getValue().compareTo(v1.getValue());//descending } }); LinkedHashMap<String, Double> sortedByComparator = new LinkedHashMap<String, Double>(); for (Entry<String, Double> e : map_arr) { sortedByComparator.put(e.getKey(), e.getValue()); } return sortedByComparator; } }