Java HashMap Sort sortHashMapByValues(HashMap passedMap)

Here you can find the source of sortHashMapByValues(HashMap passedMap)

Description

sort Hash Map By Values

License

Open Source License

Declaration

public static LinkedHashMap<String, Integer> sortHashMapByValues(HashMap<String, Integer> passedMap) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class Main {
    public static LinkedHashMap<String, Integer> sortHashMapByValues(HashMap<String, Integer> passedMap) {
        List<String> mapKeys = new ArrayList<String>(passedMap.keySet());
        List<Integer> mapValues = new ArrayList<Integer>(passedMap.values());
        Collections.sort(mapValues, new Comparator<Integer>() {

            @Override//from w  w w. j a  va  2  s  .  co  m
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2) * -1;
            }
        });
        Collections.sort(mapKeys);

        LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();

        Iterator<Integer> valueIt = mapValues.iterator();
        while (valueIt.hasNext()) {
            Object val = valueIt.next();
            Iterator<String> keyIt = mapKeys.iterator();

            while (keyIt.hasNext()) {
                Object key = keyIt.next();
                String comp1 = passedMap.get(key).toString();
                String comp2 = val.toString();

                if (comp1.equals(comp2)) {
                    passedMap.remove(key);
                    mapKeys.remove(key);
                    sortedMap.put((String) key, (Integer) val);
                    break;
                }

            }

        }
        return sortedMap;
    }
}

Related

  1. sortByValue(HashMap map)
  2. sortedScoreMap(HashMap unSortedMap)
  3. sortHashMapByKeys(HashMap passedMap, boolean ascending)
  4. sortHashMapByValues( HashMap> passedMap, String key0)
  5. sortHashMapByValues(HashMap passedMap, boolean ascending)
  6. sortMapByValue(HashMap map, final int sort)