Most of the new Stream
API features in Java 9 have already been demonstrated in the section that describes Stream
filtering. To remind you, here are the examples we have demonstrated based on the Stream
API improvements in JDK 9:
long c1 = senators.stream() .flatMap(s -> Stream.ofNullable(s.getParty() == "Party1" ? s : null)) .count(); System.out.println("OfNullable: Members of Party1: " + c1); long c2 = senators.stream() .map(s -> s.getParty() == "Party2" ? Optional.of(s) : Optional.empty()) .flatMap(Optional::stream) .count(); System.out.println("Optional.stream(): Members of Party2: " + c2); senators.stream().limit(5) .takeWhile(s -> Senate.timesVotedYes(s) < 5) .forEach(s -> System.out.println("takeWhile(<5): " + s + ": " + Senate.timesVotedYes...