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 a String

The chars method is a new one in the String class from the CharSequence interface. It’s useful for fluently iterating over the String’s characters. We can use this convenient internal iterator to apply an operation on the individual characters that make up the string. Let’s use it in an example to process a string. Along the way we’ll discuss a few more handy ways to use method references.

compare/fpij/IterateString.java
 
final​ ​String​ str = ​"w00t"​;
 
 
str.chars()
 
.forEach(ch -> ​System​.out.println(ch));

The chars method returns a Stream over which we can iterate, using the forEach internal iterator. We get direct read access to the characters in the String within the iterator. Here’s the result when we iterate and ...

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