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.

java lambda expressions examples

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.

Found the article helpful? if so please follow us on Socials