首页 > 解决方案 > HashMap的HashMap->输出问题

问题描述

具有以下 HashMap: HashMap <String, HashMap <Integer, ArrayList <Reservation> >>缓冲区;我将输出两个哈希映射的每个键的每个值。我能怎么做?

我已经写了这部分代码:

HashMap map=mod.getAllRecords();

for (Object key: map.keySet()) {
    String Date=key.toString();
    Object map2=map.get(key);

    for(Object Key : ??){//<--------------

        
    }

标签: javahashmapoutput

解决方案


我知道的最简单的方法是编写三个 for 循环 - 首先是键元素本身已经是字符串,其次是第一个 HashMap 内的 HashMap 中的整数,第三个是第二个 HashMap 的每个数组内的值:

    for (String key: map.keySet()) {
        System.out.println(key);                        //prints the strings
        HashMap <Integer, ArrayList <Reservation> > map2 = map.get(key);

        for(Integer key2 : map2.keySet()){
            System.out.println(key2);                   //prints the integers
            
            for (int i=0;i<map2.get(key2).size();i++) {
                System.out.println(map2.get(key2)[i]);  //prints everything in the array
            }
        }            
    }

推荐阅读