Java Lambda Expressions Examples
Lambda expressions in Java are a concise way to represent anonymous functions or “function literals.” They are primarily used to implement functional interfaces, which are interfaces with a single abstract method. Let’s dive into some examples.
First of all Java Lambda expressions require that you have a JDK 1.8 or higher. In your Maven pom.xml you can set the Java version with properties. For example:
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Much the same, you will need to set the Compiler version of your IDE to support Java 1.8 or higher. For example, with Eclipse select Windows > Preferences and then select Java > Installed JREs. Select Java > Compiler and set Compiler compliance level to 1.8, as shown in the picture.

Finally, let’s see some Lambda examples!
Example 1: Basic Lambda Expression
A lambda expression consists of parameters, an arrow (->), and a body. Here’s a simple example that doubles a given number:
interface NumberMultiplier {
int multiply(int number);
}
public class LambdaExample {
public static void main(String[] args) {
NumberMultiplier multiplier = (number) -> number * 2;
int result = multiplier.multiply(5);
System.out.println(result); // Output: 10
}
}
Example 2: Sorting with Lambda Expression
Lambda expressions can be useful when sorting collections. Here’s an example that sorts a list of strings based on their length:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class LambdaExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
Collections.sort(names, (str1, str2) -> str1.length() - str2.length());
System.out.println(names); // Output: [Bob, Alice, Charlie]
}
}
Example 3: Runnable Interface with Lambda Expression
Lambda expressions are commonly used with functional interfaces from the Java API. Here’s an example that demonstrates the usage of lambda with the Runnable interface:
public class LambdaExample {
public static void main(String[] args) {
Runnable runnable = () -> {
for (int i = 0; i < 5; i++) {
System.out.println("Hello, World!");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
Example 4: Filtering with Lambda Expression
Lambda expressions can also be used for filtering elements in a collection. Here’s an example that filters even numbers from a list of integers:
import java.util.ArrayList;
import java.util.List;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.removeIf(number -> number % 2 != 0);
System.out.println(numbers); // Output: [2, 4]
}
}
Example 5: Functional Interfaces with Lambda Expressions
Java provides several functional interfaces in the java.util.function package that can be used with lambda expressions. Here’s an example using the Predicate functional interface to filter strings starting with a specific letter:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class LambdaExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
Predicate<String> startsWithBPredicate = name -> name.startsWith("B");
names.removeIf(startsWithBPredicate);
System.out.println(names); // Output: [Alice, Charlie]
}
}
Conclusion
These examples should provide you with a good starting point to understand and work with lambda expressions in Java. Remember to explore more about functional interfaces and the various ways lambda expressions can be utilized in your Java programs.
Recommended Articles
Discover New Features of Java 18 and How to Install It
Learn about new JEPs in JDK 18, how to install it on your machine, and set up for Maven projects. Also explore setting UTF-8 as the default character set.
How to Resolve javax.xml.bind Errors in Java 9 and Beyond
Learn how to fix javax.xml.bind errors in Java 9+ by adding JAXB API. Includes step-by-step guidance for different versions.
Mastering Thread Dumps for Efficient Java Application Troubleshooting
Learn how to capture and analyze Java thread dumps with JDK tools, improving your application troubleshooting skills. #Java #ThreadDump #WildFly31
Enhance Java Code with JDK 13 Multiline Feature for Cleaner String Templates
Learn how to use Java's new multiline feature and combine it with string templates for cleaner, more readable code. #JavaMultiline #StringTemplates #CloudNative #Middleware #EnterpriseJava