Java Lambda Cheatsheet: Simplifying Functional Programming

In recent years, Java has undergone significant updates to embrace functional programming concepts. One of the most notable additions to the language is the introduction of lambda expressions. Lambdas allow developers to write more concise and expressive code, making it easier to work with functional interfaces. Let’s understand this topic now:

1. Lambda Expressions Basics:

A lambda expression is a concise representation of a function that can be passed around as a value. It consists of three parts: parameters, an arrow token, and a body. Here’s the general syntax:

(parameter list) -> { lambda body }

Example 1: Summing two numbers using a lambda expression

MathOperation sum = (a, b) -> a + b;
int result = sum.operation(5, 3); // Output: 8

Example 2: Filtering even numbers using a lambda expression

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbers = numbers.stream()
                                   .filter(n -> n % 2 == 0)
                                   .collect(Collectors.toList());
// Output: [2, 4, 6, 8, 10]

2. Functional Interfaces:

Functional interfaces are interfaces that contain exactly one abstract method. They serve as the basis for using lambda expressions. Some commonly used functional interfaces in Java include Predicate, Consumer, Function, and Supplier. Here’s an example of using a lambda expression with the Predicate functional interface:

Predicate<Integer> isEven = (num) -> num % 2 == 0;
boolean result = isEven.test(4); // Output: true

Example 3: Transforming a list of strings using a lambda expression

List<String> names = Arrays.asList("John", "Jane", "Alice");
List<String> uppercaseNames = names.stream()
                                   .map(String::toUpperCase)
                                   .collect(Collectors.toList());
// Output: ["JOHN", "JANE", "ALICE"]

3. Method References:

Method references provide a way to refer to methods without executing them. They can be used as shorthand for lambda expressions in specific scenarios. There are four types of method references: static, instance, class constructor, and instance constructor. Here’s an example of using a method reference:

List<String> names = Arrays.asList("John", "Jane", "Alice");
names.forEach(System.out::println);

Example 4: Sorting a list of strings using a method reference

List<String> names = Arrays.asList("John", "Jane", "Alice");
names.sort(String::compareToIgnoreCase);
// Output: ["Alice", "Jane", "John"]


Java lambda expressions provide a concise and expressive way to work with functional programming concepts. By leveraging lambda expressions, you can write cleaner and more readable code, especially when dealing with functional interfaces. This cheatsheet provides a handy reference for the syntax and usage patterns of lambda expressions, functional interfaces, and method references in Java. Experiment with these concepts to unlock the full potential of functional programming in your Java projects.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top