Card 06/ 09
All 9 cards
ConceptDifficulty: Advanced1 min
Method References, and the Case Where You Cannot Use One
A lambda whose body does nothing but call one method, passing its parameters straight through, is repeating itself. A method reference names the method and lets the compiler do the rest.
| Form | Example | The lambda it replaces |
|---|---|---|
| A static method | Integer::parseInt | s -> Integer.parseInt(s) |
| A method on a particular object | System.out::println | s -> System.out.println(s) |
| A method on whichever object arrives | String::toUpperCase | s -> s.toUpperCase() |
| A constructor | ArrayList::new | () -> new ArrayList<>() |
The third row is the one that reads oddly at first. String::toUpperCase takes no argument in the source and produces a Function<String, String>: the parameter becomes the object the method is called on.
Where you cannot use one is where the body does anything other than pass its parameters straight through — reordering them, adding a constant, negating the result, or calling two things.
names.stream().map(String::toUpperCase) // fine
names.stream().map(s -> s.toUpperCase().trim()) // two calls, so a lambda
names.stream().filter(s -> !s.isBlank()) // negated, so a lambdaUse a method reference when it fits and do not contort a lambda to make one fit. s -> !s.isBlank() is clearer than any arrangement that avoids it.