Skip to content

Instantly share code, notes, and snippets.

@eninja
Last active June 12, 2018 12:10
Show Gist options
  • Select an option

  • Save eninja/dd2ede09f3699239378b949c30e3f1ca to your computer and use it in GitHub Desktop.

Select an option

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
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