javalistjava-stream

list.stream().collect(Collectors.toList()); returns empty list


choices is a List of two elements

but choices.stream().collect(Collectors.toList()); returns an empty list

Would anyone know why?

//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


Solution

  • Look carefully at your screenshots. Your method getChoices() returns not a regular list but IndirectList which extends not a regular Collection but a Vector and that is why streams don't work as expected. This is a known bug in EclipseLink, you can read about it more here and here.

    To overcome this behaviour, you can try to update your EclipseLink version up to 2.6.0, or you may try to wrap it with a new collection, like new ArrayList<>()