java - 遍历地图而不是列表
问题描述
我呈现结果的代码如下所示:
private void presentResult(List<Long> result) {
if(result.size() == 0) {
System.out.println("No matching values for the provided query.");
}
for(String s : result) {
System.out.println(s);
}
}
但我想返回一个哈希图而不是一个列表,所以我希望它是这样的:
private void presentResult(Map<LocalDate, Long> result) {
if(result.size() == 0) {
System.out.println("No matching values for the provided query.");
}
for(Map<LocalDate, Long> s : result) {
System.out.println(s);
}
}
但后来我得到这个错误:“只能迭代数组或java.lang.Iterable的实例”如何解决?
解决方案
我想你是在问如何迭代地图,而不是列表。您可以像这样迭代地图:
for (Map.Entry<LocalDate, Long> entry : result.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
推荐阅读
- cgi - “无法解析到对象。类型和子类型 X'0203' 权限 X'0000”错误发生在通过 CGI 调用 RPGLE 程序时
- excel - 如何复制一系列数据并将其添加到该数据末尾的另一个工作簿?
- service-worker - 为什么 self.skipWaiting() 和 self.clients.claim() 不是服务人员的默认行为
- javascript - 如何修复不在 phaser.js 中加载的图像
- python - 有没有办法可视化网格?
- apache-spark-sql - 具有两列的平面图
- python - CSV 中的 URL 抓取列表给出了未知的 URL 类型错误
- java - 从活动的 onCreate 中已运行的服务中获取数据
- c - 测试和学习 C 中的图时出现意外错误
- c++ - 从 CSV 文件中读取数据并尝试将数据加载到由结构组成的向量中?