HashMap of the value (key) to sort

In order to sort HashMap in value, write a public method

Incoming HashMap <custom, custom> to, the following code is instantiated as a HashMap Map <String, String>

    /*
     * 对Map<String,String>中的value进行排序(正序)
     */
    public Map<String, String> sortMapByValue(Map<String, String> unSortMap) {
        if (unSortMap == null || unSortMap.isEmpty()) {
            return null;
        }
        List<Map.Entry<String, String>> listEntry = new ArrayList<>();
        listEntry.addAll(unSortMap.entrySet());
        Collections.sort(listEntry, new Comparator<Map.Entry<String, String>>() {
            @Override
            public intCompare (of Map.Entry <String, String> o1, of Map.Entry <String, String> o2) {
                 // String compareTo the method returns negative, before lexicographically o1 o2 in FIG. 
                return o1.getValue () compareTo (o2.getValue ());. // here. getValue into getKey sorted according to the Map Key 
            } 
        }); 
        Map <String, String> = SortedMap new new a LinkedHashMap <> ();
         for (of Map.Entry <String, String> entry: ListEntry) { 
            sortedMap.put ( entry.getKey (), entry.getValue ()); 
        } 
        return SortedMap; 
    }    

The key to the HashMap sort, simply press comments can be modified to modify getValue getKey.

 

Reference: https://www.cnblogs.com/liwei09k1/p/7722802.html

Guess you like

Origin www.cnblogs.com/Wulc-theworld/p/11424294.html