list.stream().collect(Collectors.toList());返回空列表[关闭]

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

choices 是两个元素的列表

但是

choices.stream().collect(Collectors.toList());
返回一个空列表

有人知道为什么吗?

//returns poll with list of choices
public Poll accessPoll(String pollId) {
        return pollRepository.findById(pollId).orElseThrow(
                () -> new IllegalStateException(String.format("No poll found for the ID: %s.", upperCasePollId)));
}
List<Choice> choices = pollManager.accessPoll(pollId).getChoices(); //returns list of choices
List<Choice> choices1 = pollManager.accessPoll(pollId).getChoices()
                .stream().collect(Collectors.toList()); //returns empty list

enter image description here

enter image description here

java list java-stream
1个回答
4
投票

仔细查看您的屏幕截图。您的方法

getChoices()
返回的不是常规列表,而是 IndirectList,它扩展的不是常规
Collection
而是
Vector
,这就是流无法按预期工作的原因。这是 EclipseLink 中的已知错误, 您可以在这里这里阅读更多内容。

要克服此行为,您可以尝试将 EclipseLink 版本更新到 2.6.0,或者您可以尝试用新集合包装它,例如

new ArrayList<>()

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