'Convert iterable to list using streams [duplicate]
I have a simple loop that adds elements from one list to another list.
How can the same result be achieved using streams?
List<User> users = new ArrayList<>();
for(User user: userRepository.findAll()) {
users.add(user);
}
The reason for doing so is because
userRepository.findAll() returns Iterable<User>
Solution 1:[1]
spliterator() converts Iterable in stream and then collect it as a List using collect()
List<User> users =
StreamSupport.stream(userRepository.findAll().spliterator(), false)
.collect(Collectors.toList());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Krishna |
