Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Created November 2, 2014 21:42
Show Gist options
  • Save vfarcic/15ea5de48399b0a7d6b5 to your computer and use it in GitHub Desktop.
Save vfarcic/15ea5de48399b0a7d6b5 to your computer and use it in GitHub Desktop.
package com.technologyconversations.java8exercises.streams;
import java.util.*;
import static java.util.stream.Collectors.toSet;
public class Kids {
public static Set<String> getKidNames7(List<Person> people) {
Set<String> kids = new HashSet<>();
for (Person person : people) {
if (person.getAge() < 18) {
kids.add(person.getName());
}
}
return kids;
}
public static Set<String> getKidNames(List<Person> people) {
return people.stream()
.filter(person -> person.getAge() < 18) // Filter kids (under age of 18)
.map(Person::getName) // Map Person elements to names
.collect(toSet()); // Collect values to a Set
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment