java - 如何以 Java 格式打印嵌套的 Hashmap
问题描述
我遇到了在 Java 中打印格式化到控制台的嵌套 Hashmap 的问题。我的地图结构是这样的:private static Map<String, Map<YearInterval, List<String>>> comicFilmMap = new HashMap<>();
输出应如下所示:ComicName:
Year
FilmTitle
FilmTitle
我尝试了一个 foreach 但无法让它工作。
解决方案
最简单的方法是,您可以使用 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/链接
推荐阅读
- oracle - ORACLE 中的 SQL 分析器,捕获查询
- javascript - 带有 OAuth2 的 Youtube Chrome 扩展
- c++ - 获取字符串的中间字符。如果长度是偶数,则中间两个字符
- git - 将 RStudio 项目切换到 SSH 身份验证
- string - 如何在 JavaScript 中将整数转换为字符串?
- reactjs - 错误:无法在不可变记录上设置。在反应 + 打字稿 + redux 配置
- angular - 具有键值对的 Angular TypeScript Map 未保存
- amazon-web-services - 向 Fargate 集群授予 aws s3 读取权限
- c - VS2019:VB.NET 到 C:无法加载 DLL:HRESULT:0x8007007E(在 VS2017 中工作)
- javascript - 从浏览器 JavaScript 中调用 Node 'require' 函数