Java Map Sort sortMap(Map oldMap)

Here you can find the source of sortMap(Map oldMap)

Description

sort Map

License

Apache License

Declaration

public static Map sortMap(Map oldMap) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;

import java.util.Map;

public class Main {
    public static Map sortMap(Map oldMap) {
        ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

            @Override/*from ww w . ja  v  a 2s .c o m*/
            public int compare(Map.Entry<String, Integer> arg0, Map.Entry<String, Integer> arg1) {
                return arg0.getValue() - arg1.getValue();
            }
        });
        Map newMap = new LinkedHashMap();
        for (int i = 0; i < list.size(); i++) {
            newMap.put(list.get(i).getKey(), list.get(i).getValue());
        }
        return newMap;
    }
}

Related

  1. sortKeysByValue(final Map m)
  2. sortKeysDesc(HashMap> hash)
  3. sortKeyValuePairByValue( Map map)
  4. sortMap(final Map map, final Comparator> comparator)
  5. sortMap(LinkedHashMap map, Comparator> c)
  6. sortMap(Map oldMap, final boolean asc)
  7. sortMap(Map map, int k)
  8. sortMap(Map map, Comparator> compator)
  9. sortMapByKey(Map data)