Java 8:迭代Map的所有元素 [英] Java 8: iterating across all elements of a Map
问题描述
我正在使用验证
方法返回布尔值
。
我正在使用调用此方法(Java 7),如下所示:
boolean isValid = true;
for(String key:aMap.keySet()){
isValid& = validate(key,aMap.get(key));
}
我想用Java 8重写这段代码。
Java 8允许使用以下内容迭代Map:
aMap.forEach((k, v) - > validate(k,v));
但这不起作用:
aMap.forEach((k,v) - > isValid& = validate(k,v));
问题
如何将Java 7代码重写为Java 8以获得相同的结果?
注意:
<我在这里提出了类似的问题。这篇文章的不同之处在于我想在
Map
的所有项目中进行迭代(对于 validate
构建验证报告的方法)。 isValid
必须返回 true
如果没有发生验证错误,或 false
如果至少发生了一次。你可以使用
boolean isValid = aMap.entrySet()。stream()
.map(e - > validate(e.getKey(),e.getValue()))
.reduce(true,Boolean :: logicalAnd);
as,与 anyMatch
不同, allMatch
等,减少不知道短路。但是,您执行所有验证
方法调用的要求表明该方法中存在副作用。重要的是要理解这些副作用必须是非干扰的,即这些方法调用的顺序无关紧要,甚至多个元素的并发评估也不应该破坏它。
即使满足这些要求,也不鼓励在功能操作中产生这种副作用。例如,下一个查看代码的开发人员可能会说,嘿,看起来我们应该在这里使用 allMatch
...
所以我会留在循环中。但是当处理 Map
的关联时,你不应该遍历 entrySet()
,来执行查找每个键,而是使用
boolean isValid = true;
for(Map.Entry< String,ValueType> e:aMap.entrySet())
isValid& = validate(e.getKey(),e.getValue());
从Java 10开始,您可以使用
boolean isValid = true;
for(var e:aMap.entrySet())isValid& = validate(e.getKey(),e.getValue());
消除该循环中最烦人的语法元素。
I'm having a validate
method that return a boolean
.
I'm using invoking this method (Java 7) as follow:
boolean isValid = true;
for (String key: aMap.keySet()) {
isValid &= validate(key, aMap.get(key));
}
I would like to rewrite this code in Java 8.
Java 8 allows iterating across a Map using:
aMap.forEach((k,v) -> validate(k, v));
But this won't work:
aMap.forEach((k,v) -> isValid &= validate(k, v));
Question
How can I rewrite the the Java 7 code into Java 8 to achieve the same result?
Note:
I asked a similar question here . The difference in this post is that I want to iterate this time across all the items of the Map
(for the validate
method to build a validation report). isValid
must return true
if no validation error occurred, or false
if at least one occurred.
You can use
boolean isValid = aMap.entrySet().stream()
.map(e -> validate(e.getKey(), e.getValue()))
.reduce(true, Boolean::logicalAnd);
as, unlike anyMatch
, allMatch
, etc, Reduction knows no short-circuiting. However, your requirement of executing all validate
method calls suggests that there are side-effects within that method. It’s important to understand that these side effects must be non-interfering, i.e. it should not matter in which order these method calls are made and even concurrent evaluation of multiple elements should not break it.
Even when these requirements are met, it is rather discouraged to have such side-effects in a functional operation. E.g., the next developer looking at your code may say, "hey that looks like we should use allMatch
here"…
So I’d stay with the loop. But when processing the associations of a Map
, you should not loop over the entrySet()
, to perform a lookup for every key, but rather use
boolean isValid = true;
for(Map.Entry<String, ValueType> e: aMap.entrySet())
isValid &= validate(e.getKey(), e.getValue());
Starting with Java 10, you may use
boolean isValid = true;
for(var e: aMap.entrySet()) isValid &= validate(e.getKey(), e.getValue());
eliminating the most annoying syntactical element of that loop.
这篇关于Java 8:迭代Map的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!