简体   繁体   English

如何对每个包含多个键值对的 HashMap 的 ArrayList 进行排序?

[英]How sort an ArrayList of HashMaps holding several key-value pairs each?

I need to call an external API with an ArrayList of HashMaps holding several predefined key-value pairs each.我需要使用 HashMap 的 ArrayList 调用外部 API,每个 HashMap 包含几个预定义的键值对。 An example:一个例子:

ArrayList<HashMap<String, String>> arrayListHashMap = new ArrayList<HashMap<String, String>>();

    {
        HashMap hashMap = new HashMap<String, String>();
        hashMap.put("key", "A key");
        hashMap.put("value", "B value");
        arrayListHashMap.add(hashMap);
    }

    {
        HashMap hashMap = new HashMap<String, String>();
        hashMap.put("key", "B key");
        hashMap.put("value", "A value");
        arrayListHashMap.add(hashMap);
    }

Now I need to sort this construct on the contents of the "value" key.现在我需要根据“value”键的内容对这个构造进行排序。 This sort would result in the "key=B key/value=A value" entry as the first one in the arrayListHashMap.这种排序将导致“key=B key/value=A value”条目作为arrayListHashMap 中的第一个条目。

Any help is highly appreciated.任何帮助都受到高度赞赏。

HJW华建华

You need to implement a Comparator<HashMap<String, String>> or more generally Comparator<Map<String, String>> which just extracts the value assocated with the value key, then use Collections.sort .您需要实现一个Comparator<HashMap<String, String>>或更一般的Comparator<Map<String, String>> ,它只是提取与value键关联的value ,然后使用Collections.sort Sample code (with generalization for whatever key you want to sort on):示例代码(对要排序的任何键进行泛化):

class MapComparator implements Comparator<Map<String, String>>
{
    private final String key;

    public MapComparator(String key)
    {
        this.key = key;
    }

    public int compare(Map<String, String> first,
                       Map<String, String> second)
    {
        // TODO: Null checking, both for maps and values
        String firstValue = first.get(key);
        String secondValue = second.get(key);
        return firstValue.compareTo(secondValue);
    }
}

...
Collections.sort(arrayListHashMap, new MapComparator("value"));

您可以使用以下解决方案来实现它:

arrayListHashMap.sort(Comparator.comparing(m -> m.get("value"), Comparator.nullsLast(Comparator.naturalOrder())));

(This is not an answer to the asked question - Jon did this already -, but the comment field is too small for this.) (这不是对所问问题的回答 - Jon 已经这样做了 - 但评论字段太小了。)

Your data structure looks like you misunderstood the key-value structure of maps (and Hash maps in your example).您的数据结构看起来好像您误解了映射的键值结构(以及示例中的哈希映射)。

A Map can contain any number of keys, and for each key also a value. Map 可以包含任意数量的键,并且每个键还有一个值。 A pair of key and value is given by a Map.Entry (which can be obtained by the entrySet() method of the map).一对键和值由Map.Entry给出(可以通过映射的entrySet()方法获得)。 If you then want to sort by key, simply use a SortedMap (like TreeMap) instead of the usual HashMap.如果您想按键排序,只需使用 SortedMap(如 TreeMap)而不是通常的 HashMap。

You are emulating the individual entries by a HashMap each, then putting them all in a ArrayList ... :-/您正在每个 HashMap 模拟单个条目,然后将它们全部放入 ArrayList ... :-/

Here what I would have done in your example:这是我在你的例子中所做的:

Map<String, String> map = new TreeMap<String, String>();
map.put("B key", "B value");
map.put("A key", "B value");

System.out.println(map); // already sorted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM