Java Map Sort sortByComparatorDouble( final Map map)

Here you can find the source of sortByComparatorDouble( final Map map)

Description

Descending order HashMap by value

License

Open Source License

Parameter

Parameter Description
map : HashMap<String, Double>

Declaration

public static Map<String, Double> sortByComparatorDouble(
        final Map<String, Double> map) 

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

Related

  1. sort(T map, Comparator> c)
  2. sortBy(Collection collection, final Map valuesMap)
  3. sortByComparator(Map unsortMap)
  4. sortByComparator(Map unsortMap)
  5. sortByComparatorDecreasing(Map unsortMap)
  6. sortByDescendingValues(Map unsortMap)
  7. sortByKey(final Map map, String order)
  8. sortByKey(Map map)
  9. sortByKey(T map, final boolean descending)