【问题标题】:How to print a nested Hashmap in Java formatted如何以 Java 格式打印嵌套的 Hashmap
【发布时间】:2019-11-10 07:37:28
【问题描述】:

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

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

我尝试了一个 foreach 但无法让它工作。

【问题讨论】:

    标签: java dictionary hashmap


    【解决方案1】:

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

    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/链接了解更多信息

    【讨论】:

    • 这正是我想要的。对 entryChilds 类型感到困惑。非常感谢!
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2013-12-31
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多