July 2023
Intermediate to advanced
276 pages
6h 55m
English
About an hour after I started teaching a class on functional programming for a team of developers, a programmer asked if we had any planned breaks. I quipped "In functional programming we don’t have breaks." While that comment was met with chuckles, terminating an iteration is serious business. Java provides at least two ways to exit an iteration before reaching the end of a collection—limit() and takeWhile(), where the latter was added in the JDK 9.
To only process the first three values in a collection, use the limit() function, like so:
| | friends.stream() |
| | .limit(3) |
| | .map(String::toUpperCase) |
| | .forEach(System.out::println); |
The output shows that only the first three values in the collection ...
Read now
Unlock full access