Skip to Content
Java 9 High Performance
book

Java 9 High Performance

by Mayur Ramgir, Nick Samoylov
November 2017
Intermediate to advanced
398 pages
10h 14m
English
Packt Publishing
Content preview from Java 9 High Performance

Using other stream operations for filtering

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Distributed Computing in Java 9

Distributed Computing in Java 9

Raja Malleswara Rao Malleswara Rao Pattamsetti
Mastering Java 11 - Second Edition

Mastering Java 11 - Second Edition

Dr. Edward Lavieri Jr., Mandar Jog
Java EE 8 High Performance

Java EE 8 High Performance

Romain Manni-Bucau
Clojure: High Performance JVM Programming

Clojure: High Performance JVM Programming

Eduardo Díaz, Shantanu Kumar, Akhil Wali

Publisher Resources

ISBN: 9781787120785Supplemental Content