我呈现结果的代码如下:
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的实例” 怎么解决?
答案 0 :(得分:0)
我认为您是在问如何迭代地图,而不是列表。您可以像这样迭代地图:
for (Map.Entry<LocalDate, Long> entry : result.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
答案 1 :(得分:0)
您需要使用result.entrySet()
。返回Set<Entry<LocalDate, Long>>>
,它是可迭代的(不是Map)。
您的循环如下所示:
for (Entry<LocalDate, Long> s : result.entrySet()) {
System.out.println(s.getKey() + " - " + s.getValue());
}
答案 2 :(得分:0)
您应使用地图的entrySet。
for(Map.Entry<LocalDate, Long> s : result.entrySet)
{
System.out.println(s.getKey());
System.out.println(s.getValue());
}