Last 30 Days
No notifications
A lambda is a short way to write an anonymous function. Java added them in 8 to enable functional-style code (streams, callbacks, event handlers). Every lambda is really an instance of a *functional interface* — an interface with exactly one abstract method. The standard set lives in java.util.function (Function, Predicate, Consumer, Supplier, …).
# Lambdas & Functional Interfaces
() -> 42 // no params
x -> x * x // single param, no parens needed
(int a, int b) -> a + b // explicit types
(a, b) -> { // multi-line body
int sum = a + b;
return sum;
}@FunctionalInterface makes the compiler verify it.@FunctionalInterface
interface Greeter {
String greet(String name); // the single abstract method
}Greeter g = name -> "hi " + name; // lambda assigned to it
System.out.println(g.greet("alice")); // hi alice
java.util.function)| interface | shape | example |
Function | T → R | String::length |
Predicate | T → boolean | s -> s.isEmpty() |
Consumer | T → void | System.out::println |
Supplier | () → T | () -> new ArrayList<>() |
BiFunction | (T, U) → R | (a, b) -> a + b |
UnaryOperator | T → T | x -> x.toUpperCase() |
BinaryOperator | (T, T) → T | Integer::sum |
| form | example | equivalent lambda |
| static method | Integer::parseInt | s -> Integer.parseInt(s) |
| instance of object | out::println | x -> out.println(x) |
| instance of type | String::length | s -> s.length() |
| constructor | ArrayList::new | () -> new ArrayList<>() |
list.forEach(System.out::println);
list.removeIf(s -> s.isBlank());
list.sort(Comparator.comparingInt(String::length));
new Thread(() -> System.out.println("running")).start();
button.addActionListener(e -> doStuff());int multiplier = 10;
Function<Integer,Integer> times = x -> x * multiplier; // ok
// multiplier = 20; <-- would make the line above fail to compileFunction<Integer,Integer> add1 = x -> x + 1;
Function<Integer,Integer> dbl = x -> x * 2;add1.andThen(dbl).apply(3); // (3+1)*2 = 8
add1.compose(dbl).apply(3); // (3*2)+1 = 7
Predicate<String> nonEmpty = s -> !s.isEmpty();
Predicate<String> short_ = s -> s.length() < 5;
nonEmpty.and(short_).test("hi"); // true
nonEmpty.or(short_).negate().test(""); // false