在列表中查找元素并使用stream()更改它

use*_*986 3 java java-8 java-stream

是否可以使用Java 8流在a中找到元素List,更改它或者在找不到元素时抛出Exception

换句话说,我想用下面的代码重写代码stream.我能得到的最好的是更改项目值,但无法确定是否找到/更改了项目.

boolean isFound = false;
for (MyItem item : myList) {
    if (item.getValue() > 10) {
        item.setAnotherValue(5);
        isFound = true;
    }
}

if (!isFound) {
    throw new ElementNotFoundException("Element 10 wasn't found");
}
Run Code Online (Sandbox Code Playgroud)

use*_*547 7

如果您的目标是只找到一个元素,那么您可以这样做

MyItem item = l.stream()
                .filter(x -> x.getValue() > 10)
                .findAny()  // here we get an Optional
                .orElseThrow(() -> new RuntimeException("Element 10 wasn't found"));
        item.setAnotherValue(4);
Run Code Online (Sandbox Code Playgroud)

在Java 9中,使用ifPresentOrElse,这可以稍微简化一下(遗憾的是语法()->{throw new RuntimeException();}也有点笨拙,但AFAIK却无法简化):

l.stream()
    .filter(x -> x.getValue() > 10)
    .findAny() // here we get an Optional
    .ifPresentOrElse(x->x.setAnotherValue(5),
            ()->{throw new RuntimeException();});
Run Code Online (Sandbox Code Playgroud)

如果你想为所有项目做这件事,你可以尝试这样的事情.但由于Java 8 Streams不是为了通过副作用而设计的,所以这不是一个非常干净的方法:

AtomicBoolean b = new AtomicBoolean(false);
l.stream()
        .filter(x -> x.getValue() > 10)
        .forEach(x->{
            x.setAnotherValue(5);
            b.set(true);
        });
if (b.get()){
   throw new RuntimeException();
}
Run Code Online (Sandbox Code Playgroud)

当然,您也可以简单地将元素收集到列表中,然后执行操作.但我不确定这是否比你开始使用的简单for循环有任何改进......

好吧,如果forEach返回一个long代表它被调用的元素数量,这将更容易......