Last active
June 12, 2018 12:10
-
-
Save eninja/dd2ede09f3699239378b949c30e3f1ca to your computer and use it in GitHub Desktop.
From List<Person> (when Person has String fields: name, surname, email) return emails with only one occurence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashSet; | |
| import java.util.List; | |
| import java.util.Set; | |
| import java.util.stream.Collectors; | |
| public class UtilEmail { | |
| public Set<String> getEmailsWithOneOccurence(List<Person> people) { | |
| List<String> emails = people.stream().map(Person::getEmail).collect(Collectors.toList()); | |
| Set<String> emailsUnique = new HashSet<>(); | |
| Set<String> emailsDuplicates = new HashSet<>(); | |
| for (String email : emails) { | |
| if (!emailsUnique.add(email)) { | |
| emailsDuplicates.add(email); | |
| } | |
| } | |
| emailsUnique.removeAll(emailsDuplicates); | |
| return emailsUnique; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment