I found a template very useful to allow the parent class (in this case the Contact class) to create and return streams of child objects (in this case, share friends tags):
public class Contact { private List<String> sharedFriendsIds; public Stream<String> sharedFriendsIds() { return sharedFriendsIds == null ? Stream.empty() : sharedFriendsIds.stream(); } public List<String> getSharedFriendsIds() { return sharedFriendsIds; } }
The convention is to name a method that returns a stream as the stream passed by the attribute. This method already contains a null check.
Then getting common friends ids for all contacts is much simpler:
List<String> sharedContacts = contactsList.stream() .flatMap(Contact::sharedFriendsIds) .collect(Collectors.toList());
You need to use flatMap() to smooth the elements of the child list into a single list, otherwise you will get a list of threads.
Note 1: you do not need to use sequential() since using stream() in your contact list already returns a serial stream.
Note 2: if you want the final list to be sorted, you must use sorted() in the stream:
List<String> sharedContacts = contactsList.stream() .flatMap(Contact::sharedFriendsIds) .sorted() .collect(Collectors.toList());
Federico peralta schaffner
source share