Java Lambda Expression Cheat Sheet

Introduction to Lambda Expressions



Lambda Expression Syntax


(parameters) -> expression (parameters) -> { statements; }

Example


(int x, int y) -> x + y

Components


  • Parameters: Comma-separated list of input parameters.
  • Arrow Operator (->): Separates the parameters from the body.
  • Body: Can be a single expression or a block of statements.

Functional Interfaces



Functional Interface


  • An interface with exactly one abstract method.
  • Can be annotated with @FunctionalInterface (optional).
  • Examples: Runnable, Callable, Comparator, ActionListener, etc.

Example


Java
Copy
@FunctionalInterface
interface MyFunctionalInterface {
    void doSomething();
}

Common Functional Interfaces

  • Predicate<T>: boolean test(T t)
  • Function<T, R>: R apply(T t)
  • Consumer<T>: void accept(T t)
  • Supplier<T>: T get()
  • UnaryOperator<T>: T apply(T t)
  • BinaryOperator<T>: T apply(T t1, T t2)

Lambda Expression Examples



Without Parameters () -> System.out.println("Hello, World!");

With One Parameter x -> x * x

With Multiple Parameters (a, b) -> a + b

With Block of Statements (int x, int y) -> { int sum = x + y; return sum; }

Using Functional Interface MyFunctionalInterface myFunction = () -> System.out.println("Doing something!"); myFunction.doSomething();

Using Common Functional Interfaces


Predicate Example Predicate isEmpty = s -> s.isEmpty();

Function Example Function length = s -> s.length();

Consumer Example Consumer print = s -> System.out.println(s);

Supplier Example Supplier random = () -> Math.random();

Method References



Method Reference Syntax ClassName::methodName

Types of Method References

Static Method Reference Function toString = String::valueOf;

Instance Method Reference of a Particular Object Consumer printer = System.out::println;

Instance Method Reference of an Arbitrary Object of a Particular Type Predicate isEmpty = String::isEmpty;

Constructor Reference Supplier> listSupplier = ArrayList::new;

Capturing Lambdas



Accessing Local Variables


Lambdas can capture and use variables from the surrounding context (effectively final). int num = 10; Supplier supplier = () -> num;

Effectively Final:
The variable must not be modified after it is assigned.

Practical Examples



Filtering a List using a Lambda Expression

Java
Copy
List<String> names = Arrays.asList("John", "Jane", "Jack");
List<String> filteredNames = names.stream()
    .filter(name -> name.startsWith("J"))
    .collect(Collectors.toList());

Sorting a List using a Lambda Expression


Java
Copy
List<String> names = Arrays.asList("John", "Jane", "Jack");
Collections.sort(names, (a, b) -> a.compareTo(b));

Iterating over a List using a Lambda Expression


Java
Copy
List<String> names = Arrays.asList("John", "Jane", "Jack");
names.forEach(name -> System.out.println(name));

Using Lambdas with Streams



Map and Reduce Example


Java
Copy
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
    .map(x -> x * x)
    .reduce((x, y) -> x + y)
    .orElse(0);

Filter and Collect Example


Java
Copy
List<String> names = Arrays.asList("John", "Jane", "Jack", "Doe");
List<String> filteredNames = names.stream()
    .filter(name -> name.startsWith("J"))
    .collect(Collectors.toList());