Skip to Content
Functional Programming in Java
book

Functional Programming in Java

by Venkat Subramaniam
February 2014
Intermediate to advanced
160 pages
4h 59m
English
Pragmatic Bookshelf
Content preview from Functional Programming in Java

Finding Elements

The now-familiar elegant methods to traverse and transform collections will not directly help pick elements from a collection. The filter method is designed for that purpose.

From a list of names, let’s pick the ones that start with the letter N. Since there may be zero matching names in the list, the result may be an empty list. Let’s first code it using the old approach.

collections/fpij/PickElements.java
 
final​ ​List​<​String​> startsWithN = ​new​ ​ArrayList​<​String​>();
 
for​(​String​ name : friends) {
 
if​(name.startsWith(​"N"​)) {
 
startsWithN.add(name);
 
}
 
}

That’s a chatty piece of code for a simple task. We created a variable and initialized it to an empty collection. Then we looped through the collection, looking ...

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

Functional Programming in Java

Functional Programming in Java

Pierre-Yves Saumont

Publisher Resources

ISBN: 9781941222690Errata Page