Created
November 2, 2014 21:42
-
-
Save vfarcic/15ea5de48399b0a7d6b5 to your computer and use it in GitHub Desktop.
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
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