Alternatively, or in addition to the basic filtering described in the previous section, other operations (methods of the Stream interface) can be used for selection and filtering emitted stream elements.
For example, let's use the flatMap() method to filter out the members of the Senate by their party membership:
long c1 = senators.stream() .flatMap(s -> s.getParty() == "Party1" ? Stream.of(s) : Stream.empty()) .count();System.out.println("Members of Party1: " + c1);
This method takes advantage of the Stream.of() (produces a stream of one element) and Stream.empty() factory methods (it produces a stream without elements, so nothing is emitted further downstream). Alternatively, the same effect ...