如何以Java格式打印嵌套的Hashmap

时间:2019-11-10 07:37:28

标签: java dictionary hashmap

我在打印格式化为Java控制台的嵌套Hashmap时遇到问题。我的地图结构是这样的:private static Map<String, Map<YearInterval, List<String>>> comicFilmMap = new HashMap<>();

输出应如下所示:ComicName: Year FilmTitle FilmTitle

我尝试了一次foreach,但无法正常工作。

1 个答案:

答案 0 :(得分:0)

最简单的方法是,您可以使用Map.Entry接口遍历哈希映射。 下面是相同的伪代码:

foreach(Entry entry: comicFileMap.entryset()){
  sysout(entry.getKey()); // this will be your comicName

  foreach(Entry entryChild: entry.getValue()){ //the getValue() will be again 
                                               //hashmap()
     sysout(entrychild.getkey());
     foreach(String str: entryChild.getValue()){//this loop will print list of 
                                                //string
       sysout(str); 
     }
  }
}

有关更多信息,您可以使用 https://www.geeksforgeeks.org/map-entry-interface-java-example/ 链接