Java 8:迭代一些Map元素

Java 8:迭代一些Map元素

问题描述:

我正在使用验证方法返回布尔值

我正在调用此方法(Java 7),如下所示:

I'm invoking this method (Java 7) as follow:

    boolean isValid = true;
    for (String key: aMap.keySet()) {
        isValid &= validate(key, aMap.get(key));
        if(!isValid) {
            break;
        }
    }

我想在Java 8中重写这段代码。

I would like to rewrite this code in Java 8.

Java 8允许使用以下内容迭代Map:

Java 8 allows iterating across a Map using:

aMap.forEach((k,v) -> validate(k, v));

但这不起作用:

aMap.forEach((k,v) -> isValid &= validate(k, v)); 

问题

如何将Java 7代码重写为Java 8以获得相同的结果?

How can I rewrite the the Java 7 code into Java 8 to achieve the same result?

注意

提出类似问题 here (但这次,迭代继续通过所有 Map 元素)

Raised a similar question here (but this time, for the iteration to continue through all the Map elements)

boolean isValid = aMap.keySet()
                .stream()
                .allMatch(key -> validate(key, paramRow.getRowMap().get(colName))

作为旁注,是什么? paramRow.getRowMap()。get(colName) do?你在哪里得到 colName ?可能你不必重新计算这个每一个键

As a side note, what does paramRow.getRowMap().get(colName) do? And where do you get colName? May be you don't have to recompute this for every single key