Sub topic 1
Refresher: Lambdas and Method References
Stream operations take small pieces of behavior as arguments. You might pass "keep words longer than three letters" or "turn this word into upper case." In Java, you write those pieces as lambdas or method references. This chapter reviews both so the stream code in later chapters is easy to read.
What a Lambda Is
A lambda is a short, unnamed function that you can store in a variable or pass to a method. It has three parts:
- The parameters, such as
word. - An arrow,
->. - The body, which is the work to do, such as
word.length() > 3.
If the body is a single expression, Java returns its value for you. You do not write return or curly braces.
A lambda always fills in a functional interface. A functional interface is an interface with exactly one abstract method. Java ships many of them in the java.util.function package. These are the ones you will see most with streams:
| Interface | Method | What it does | Example lambda |
|---|---|---|---|
| `Predicate<T>` | `test` | Takes a value, returns true or false | `w -> w.isEmpty()` |
| `Function<T, R>` | `apply` | Takes a value, returns a new value | `w -> w.length()` |
| `Consumer<T>` | `accept` | Takes a value, returns nothing | `w -> System.out.println(w)` |
| `Supplier<T>` | `get` | Takes nothing, returns a value | `() -> "default"` |
What a Method Reference Is
A method reference is a shorter way to write a lambda that only calls one existing method. It uses a double colon, ::. For example, word -> word.toUpperCase() can be written as String::toUpperCase. Both do the same thing.
| Kind | Method reference | Same as this lambda |
|---|---|---|
| Static method | `Integer::parseInt` | `s -> Integer.parseInt(s)` |
| Instance method of the element | `String::length` | `s -> s.length()` |
| Method of a specific object | `System.out::println` | `x -> System.out.println(x)` |
| Constructor | `ArrayList::new` | `() -> new ArrayList<>()` |
A Worked Example
This program builds a predicate and two functions, then calls them directly.
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
public class LambdaRefresher {
public static void main(String[] args) {
Predicate<String> isLong = word -> word.length() > 3;
System.out.println(isLong.test("kiwi")); // true
System.out.println(isLong.test("fig")); // false
Function<String, String> shout = word -> word.toUpperCase();
Function<String, String> shoutRef = String::toUpperCase;
System.out.println(shout.apply("hello")); // HELLO
System.out.println(shoutRef.apply("hello")); // HELLO
List<String> names = List.of("alice", "bob");
names.forEach(System.out::println); // alice, then bob
}
}The shout and shoutRef variables behave the same way. The method reference is shorter, and it names the method plainly.
Exercise
Rewrite the lambda s -> Integer.parseInt(s) as a method reference, and name the functional interface that fits it if the input is a String and the output is an Integer.
Show answer
The method reference is Integer::parseInt. It fits Function<String, Integer>, because it takes one String and returns a value.
Exercise
Can n -> n * 2 be written as a method reference? Explain why or why not.
Show answer
No. The lambda does arithmetic. It does not just call one existing method with its parameter, so no method reference replaces it.