有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

字典如何打印Java格式的嵌套哈希映射

我遇到了一个问题:用Java将格式化的嵌套哈希映射打印到控制台。我的地图结构如下:private static Map<String, Map<YearInterval, List<String>>> comicFilmMap = new HashMap<>();

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

我试过用foreach,但没能成功


共 (1) 个答案

  1. # 1 楼答案

    最简单的方法是,你可以使用地图。输入接口,以在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/链接