For example: I have a list of user objects with e-mails. I would like to collect the list of users with distinct e-mails (because two users with the same e-mail would be pro
You can use Collectors.toMap(keyMapper, valueMapper, mergeFunction), use the e-mail as key, user as value and ignore the key conflicts by always returning the first value. From the Map
, you can then get the users by calling values().
public List getUsersToInvite(List users) {
return new ArrayList<>(users.stream()
.collect(Collectors.toMap(User::getEmail,
Function.identity(),
(u1, u2) -> u1))
.values());
}