在Java中按键或值对地图排序

时间:2019-02-27 15:43:06

标签: java sorting dictionary hashmap

按键或按Java中的值对地图进行排序

输入地图1-eee 4-ddd 5-ccc 0-bbb 3-aaa

第一个输出映射(按键):0-bbb 1-eee 3-aaa 4-ddd 5-ccc
第二个输出映射(按值):3-aaa 0-bbb 5-ccc 4-ddd 1-eee

2 个答案:

答案 0 :(得分:0)

此代码将首先按Key对地图进行排序,然后按值对地图进行排序。
只需编写一个main方法并按如下所示调用此方法:

SELECT n.nid, n.vid, n.title
FROM node_revision n
INNER JOIN
   (SELECT nid, MAX(vid) as max_vid 
    FROM node_revision 
    GROUP BY nid
   ) AS agg
ON agg.nid = n.nid AND agg.max_vid = n.vid

答案 1 :(得分:-1)

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;
import java.util.Set;

public class MapSortUtil
{
    public static Map<String, String> sortMapByKey(Map<String, String> anUnSortedMap)
    {
        List<Entry<String, String>> myListOfEntrySet = getListOfEntrySetFromMap(anUnSortedMap);

        /* Sort the list of entry-set in ascending order. */
        Collections.sort(myListOfEntrySet, new MapComparatorToSortByKey());

        /* Generating new Map from Sorted Entry List. */
        Map<String, String> mySortedMap = getSortedMapFromSortedEntry(myListOfEntrySet);

        return mySortedMap;
    }

    public static Map<String, String> sortMapByValue(Map<String, String> anUnSortedMap)
    {
        List<Entry<String, String>> myListOfEntrySet = getListOfEntrySetFromMap(anUnSortedMap);

        /* Sort the list of entry-set in ascending order. */
        Collections.sort(myListOfEntrySet, new MapComparatorToSortByValue());

        /* Generating new Map from Sorted Entry List. */
        Map<String, String> mySortedMap = getSortedMapFromSortedEntry(myListOfEntrySet);

        return mySortedMap;
    }

    private static List<Entry<String, String>> getListOfEntrySetFromMap(Map<String, String> anUnSortedMap)
    {
        /* Getting Entry Set from the Map. */
        Set<Entry<String, String>> myEntrySetOfMap = anUnSortedMap.entrySet();

        /* Creating List of Entry Set. */
        List<Entry<String, String>> myListOfEntrySet = new LinkedList<Map.Entry<String, String>>(myEntrySetOfMap);

        return myListOfEntrySet;
    }

    private static Map<String, String> getSortedMapFromSortedEntry(List<Entry<String, String>> myListOfEntrySetOfMap)
    {
        /* Add Sorted list in new LinkedHashMap one-by-one. */
        Map<String, String> mySortedLinkedHashMap = new LinkedHashMap<String, String>();
        for (Entry<String, String> myOneByOneData : myListOfEntrySetOfMap)
        {
            mySortedLinkedHashMap.put(myOneByOneData.getKey(), myOneByOneData.getValue());
        }

        return mySortedLinkedHashMap;
    }
}

class MapComparatorToSortByValue implements Comparator<Entry<String, String>>
{
    @Override
    public int compare(Entry<String, String> aMap1, Entry<String, String> aMap2)
    {
        return aMap1.getValue().compareTo(aMap2.getValue());
    }
}

class MapComparatorToSortByKey implements Comparator<Entry<String, String>>
{
    @Override
    public int compare(Entry<String, String> aMap1, Entry<String, String> aMap2)
    {
        return aMap1.getKey().compareTo(aMap2.getKey());
    }
}