July 2023
Intermediate to advanced
276 pages
6h 55m
English
It’s reasonable to expect that picking one element from a collection would be simpler than picking multiple elements. But there are a few complications. Let’s look at the complexity introduced by the habitual approach and then bring in lambda expressions to solve it.
Let’s create a method that will look for an element that starts with a given letter, and print it.
| | public static void pickName( |
| | final List<String> names, final String startingLetter) { |
| | String foundName = null; |
| | for(String name : names) { |
| | if(name.startsWith(startingLetter)) { |
| | foundName = name; |
| | break; |
| | } |
| | } |
| | System.out.print(String.format("A name starting with %s: ", startingLetter)); |
| | |
| | ... |
Read now
Unlock full access