简体   繁体   English

对HashMap及其嵌套的HashMap进行排序

[英]Sorting HashMap and its nested HashMap

I'm trying to sort nested hashMaps , where both HashMaps need to be sorted. 我正在尝试对嵌套的hashMaps进行排序,其中两个HashMaps都需要进行排序。

The first hashMap should be sorted by key. 第一个hashMap应该按键排序。 Second one should be sorted by value. 第二个应该按值排序。 So far I manage to sort the keys of the first hashMap by making an array of the keys and sort it . 到目前为止,我设法通过对键进行数组排列并对第一个hashMap的键进行排序。

My hashmap looks like this 我的哈希图看起来像这样

 HashMap<String,HashMap<String,Integer>> carOwners = new 
                                    HashMap<String,HashMap<String, Integer>>();

an Example is let say we have first String is Name , car brand, qnty. 举例来说,假设我们的第一个String是Name,car brand,qnty。

Example: David: 例如:David:

Audi>5 奥迪> 5

Bmw>4 宝马> 4

Izabel: 伊莎贝尔:

VW>10 大众> 10

MB>4 MB> 4

So Basicly first we sort the names, and then we sort the nested hash by value. 因此,基本上,我们首先对名称进行排序,然后对嵌套哈希值按值进行排序。 How can this be done ... cant find any useful information :(> 如何做到这一点...找不到任何有用的信息:(>

For sorting a Map you can use the following class: TreeMap . 要对Map排序,可以使用以下类: TreeMap As the official documentation says, 官方文件所述

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. 根据映射键的自然顺序或在映射创建时提供的Comparator对映射进行排序,具体取决于所使用的构造函数。

If you want to sort the elements in insertion order , please use LinkedHashMap . 如果要按插入顺序对元素进行排序 ,请使用LinkedHashMap

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). 此链表定义了迭代顺序,通常是将键插入映射的顺序(插入顺序)。

For sorting a Map by value, please see this post . 有关按值对Map进行排序的信息,请参见这篇文章 It's also for Java7 and Java8. 它也适用于Java7和Java8。

Hope it helps. 希望能帮助到你。

Make the first map as TreeMap and for the second Map, sort it by values. 将第一个地图设为TreeMap,将第二个地图设为按值排序。 Refer this post 推荐这个职位

Below is the code snippet for your problem. 以下是您的问题的代码段。

public static void main(String[] args) {
    Map<String, HashMap<String, Integer>> carOwners = new TreeMap<String, HashMap<String, Integer>>();
    HashMap<String, Integer> nameQuantity = new HashMap<String, Integer>();
    nameQuantity.put("Audi", 5);
    nameQuantity.put("BMW", 4);
    carOwners.put("David", sortByValue(nameQuantity)); 
    nameQuantity = new HashMap<String, Integer>();
    nameQuantity.put("VW", 10);
    nameQuantity.put("MB", 4);
    carOwners.put("Izabel", sortByValue(nameQuantity)); 
    for (Map.Entry<String, HashMap<String, Integer>> carOwnerEntry : carOwners.entrySet()) {
        System.out.println(carOwnerEntry.getKey());
        HashMap<String, Integer> nameQty = carOwnerEntry.getValue();
        for (Map.Entry<String, Integer> nameQtyEntry : nameQty.entrySet()) {
            System.out.println(nameQtyEntry.getKey() + " " + nameQtyEntry.getValue());
        }
    }

public static <K, V extends Comparable<? super V>> HashMap<K, V> sortByValue(Map<K, V> map) {
    List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        });
    HashMap<K, V> result = new LinkedHashMap<K, V>();
    for (Map.Entry<K, V> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

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

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