Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Created November 2, 2014 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfarcic/c66cf0e2be689b510356 to your computer and use it in GitHub Desktop.
Save vfarcic/c66cf0e2be689b510356 to your computer and use it in GitHub Desktop.
package com.technologyconversations.java8exercises.streams;
import java.util.*;
import static java.util.stream.Collectors.*;
public class Partitioning {
public static Map<Boolean, List<Person>> partitionAdults7(List<Person> people) {
Map<Boolean, List<Person>> map = new HashMap<>();
map.put(true, new ArrayList<>());
map.put(false, new ArrayList<>());
for (Person person : people) {
map.get(person.getAge() >= 18).add(person);
}
return map;
}
public static Map<Boolean, List<Person>> partitionAdults(List<Person> people) {
return people.stream() // Convert collection to Stream
.collect(partitioningBy(p -> p.getAge() >= 18)); // Partition stream of people into adults (age => 18) and kids
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment