Java HashMap Sort sortByValue(HashMap map)

Here you can find the source of sortByValue(HashMap map)

Description

sort By Value

License

Apache License

Declaration

public static HashMap sortByValue(HashMap map) 

Method Source Code

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

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    public static HashMap sortByValue(HashMap map) {
        List list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
            }/*from   w w w .j  av  a 2  s .  c  om*/
        });

        HashMap result = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
}

Related

  1. getSortedColors(HashMap colors)
  2. getSortedHashMapKeyset(Map sorting)
  3. getSortedLinkedHashMap(Map bitCounts, Comparator comparator)
  4. sortByComparator(HashMap unsortMap, final boolean ascendingorder)
  5. sortedScoreMap(HashMap unSortedMap)
  6. sortHashMapByKeys(HashMap passedMap, boolean ascending)
  7. sortHashMapByValues( HashMap> passedMap, String key0)
  8. sortHashMapByValues(HashMap passedMap, boolean ascending)

  9. HOME | Copyright © www.java2s.com 2016