Java Reference
In-Depth Information
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.mapping;
public class DobCalendar {
public static void main(String[] args) {
Map<Month, String> dobCalendar = Person.persons()
.stream().collect(collectingAndThen(
groupingBy(p -> p.getDob().getMonth(),
mapping(Person::getName, joining(", "))),
result -> {
// Add missing months
for (Month m : Month.values()) {
result.putIfAbsent(m, "None");
}
// Return a sorted, unmodifiable map
return Collections.unmodifiableMap(new TreeMap<>(result));
}));
dobCalendar.entrySet().forEach(System.out::println);
}
}
JANUARY=None
FEBRUARY=None
MARCH=None
APRIL=None
MAY=Ken, Li
JUNE=None
JULY=Jeff, Donna
AUGUST=None
SEPTEMBER=None
OCTOBER=None
NOVEMBER=None
DECEMBER=Chris, Laynie
Finding and Matching in Streams
The Streams API supports different types of find and match operations on stream elements. For example, you can
check if any elements in the stream match a predicate, if all elements match a predicate etc. The following methods in
the Stream interface are used to perform find and match operations:
boolean allMatch(Predicate<? super T> predicate)
boolean anyMatch(Predicate<? super T> predicate)
boolean noneMatch(Predicate<? super T> predicate)
 
Search WWH ::




Custom Search