With java 8, now we can use the java.util.Collection.stream
to do some collection basic functions such as filter
, map
, collect
, anyMatch
, etc.
The problem is when you have a Collection and you have to use some of this functions, the code gets a little ugly, for example:
List<String> ids = this.getFriends(userId).stream().map(Friend::getFriendUserId).collect(Collectors.toList())
In this case, I have a List<Friend>
and I want to have a List<String>
that gets populated with the friendId of every item in the first List.
What I think is that maybe the code could be like this:
List<String> ids = this.getFriends(userId).map(Friend::getFriendUserId);
This of course is more readable and also more clean, but invalid as List
does not understand the message map. I can't understand what would be the problem of implementing this in Java 8, and why do they decide to force every time to convert to Stream
when in some cases we then convert it back to a Collection
as in this case.