java.lang.IllegalStateException:流已被操作或关闭,对于多个过滤器语句

时间:2019-11-04 07:55:35

标签: java java-stream illegalstateexception

在执行java.lang.IllegalStateException: stream has already been operated upon or closed时获取findFirst()。有人知道为什么吗?

public ResultCode getCodeBySourceTypeScope(final List<CodeResult> results,
                                                 final String source, final String type,
                                                 final String scope) {
    if (results.isEmpty()){
        return null;
    }
    final Stream<ResultCode> resultStream = results
            .get(0)
            .getCodes()
            .stream();

    if(source != null){
        resultStream.filter(code -> code.getSource().equals(source));
    }

    if(type != null){
        resultStream.filter(code -> code.getType().equals(type));
    }

    if(scope != null){
        resultStream.filter(code -> String.valueOf(code.getScope()).equals(scope));
    }

    return resultStream
            .findFirst()
            .orElse(null);
}

0 个答案:

没有答案
相关问题