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

Iterating through a List

Iterating through a list is a basic operation on a collection, but over the years it’s gone through a few significant changes. We’ll begin with the old and evolve an example—enumerating a list of names—to the elegant style.

We can easily create an immutable collection of a list of names with the following code:

 
final​ ​List​<​String​> friends =
 
Arrays​.asList(​"Brian"​, ​"Nate"​, ​"Neal"​, ​"Raju"​, ​"Sara"​, ​"Scott"​);

Here’s the habitual, but not so desirable, way to iterate and print each of the elements.

collections/fpij/Iteration.java
 
for​(​int​ i = 0; i < friends.size(); i++) {
 
System​.out.println(friends.get(i));
 
}

I call this style the self-inflicted wound pattern—it’s verbose and error prone. We ...

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