流迭代列表上出现 NoSuchElementException<Object>

问题描述 投票:0回答:1

嗨! 尝试使用此流,但我遇到了 NoSuchElementException

var scores = findAllByBarcode
                .stream()
                .iterator()
                .next()
                        .getMpProductFeedbacks()
                                .stream()
                                        .filter(mpProductFeedback -> mpProductFeedback.getId()==mpProductFeedbackId)
                                               .iterator()
                .next()
                .getMpPersonScores()
                .stream()
                .map(mpPersonScore -> {
                            var score = new ScoreType();
                            score.setUserScore(mpPersonScore.getMpPersonScore());
                            score.setUser(mpPersonScore.getMpPersonLogin());
                            score.setUserComment(mpPersonScore.getMpPersonComment());
                            return score;
                })
    .toList();

部分后出现异常

filter(mpProductFeedback -> mpProductFeedback.getId()==mpProductFeedbackId)
                                           .iterator()
            .next()

尝试使用 orElse,但它仅适用于 .equals 我已经在两个 long 之间有 == 了。

java java-stream nosuchelementexception
1个回答
0
投票

嗯,听起来没有满足条件的元素。考虑使用

findFirst()
而不是
.iterator().next()
来正确处理
Optional

© www.soinside.com 2019 - 2024. All rights reserved.