WikiGalaxy

Personalize

Java Lambda Expressions

Introduction to Lambda Expressions:

Lambda expressions are a feature of Java that allow you to write concise and functional-style code. They enable you to implement functional interfaces with a simpler syntax.

Syntax of Lambda Expressions:

The basic syntax is (parameters) -> expression or (parameters) -> { statements }. This provides a clear and readable way to represent instances of single-method interfaces.


      interface MathOperation {
          int operation(int a, int b);
      }
      MathOperation addition = (a, b) -> a + b;
      System.out.println("Addition: " + addition.operation(5, 3));
    

Functional Interfaces:

A functional interface is an interface with a single abstract method. Lambda expressions work with functional interfaces to provide the implementation of that method.

Console Output:

Addition: 8

Lambda with Collections

Using Lambdas with Collections:

Lambda expressions can be used to iterate over collections in a concise manner, often replacing the need for anonymous inner classes.


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

Benefits of Using Lambdas:

Lambdas simplify the code by reducing boilerplate, making it easier to read and maintain. They are especially useful in writing concise logic for collection processing.

Console Output:

John

Jane

Doe

Lambda with Streams

Lambda and Stream API:

The Stream API in Java 8 allows for functional-style operations on streams of elements. Lambdas are used extensively with streams to perform operations like filter, map, and reduce.


      List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
      numbers.stream()
          .filter(n -> n % 2 == 0)
          .forEach(System.out::println);
    

Stream Operations:

Stream operations are divided into intermediate and terminal operations. Lambdas are often used in intermediate operations like filter and map to transform data.

Console Output:

2

4

Lambda for Event Handling

Event Handling with Lambdas:

Lambdas can be used in GUI applications to handle events in a more compact and expressive way compared to anonymous inner classes.


      Button button = new Button("Click Me");
      button.setOnAction(e -> System.out.println("Button Clicked!"));
    

Advantages in GUI:

Using lambdas for event handling reduces verbosity and improves the readability of GUI-related code, making it easier to manage and understand.

Console Output:

Button Clicked!

Lambda with Comparator

Using Lambda for Custom Sorting:

Lambdas can be used to implement custom sorting logic in a concise manner, particularly with the Comparator interface.


      List<String> names = Arrays.asList("John", "Jane", "Doe");
      Collections.sort(names, (a, b) -> a.compareTo(b));
      names.forEach(System.out::println);
    

Simplifying Comparators:

Lambdas simplify the creation of comparator objects, reducing the need for boilerplate code and enhancing readability.

Console Output:

Doe

Jane

John

Lambda with Runnable

Using Lambda for Runnable:

Lambdas can be used to implement the Runnable interface, providing a more concise way to create threads.


      new Thread(() -> System.out.println("Thread is running")).start();
    

Benefits in Multithreading:

Using lambdas for Runnable reduces the verbosity of thread creation and enhances the clarity of multithreaded code.

Console Output:

Thread is running

Lambda with Predicate

Lambda with Predicate Interface:

The Predicate interface is a functional interface that represents a boolean-valued function. Lambdas can be used to define predicates in a concise way.


      Predicate<Integer> isEven = n -> n % 2 == 0;
      System.out.println("Is 4 even? " + isEven.test(4));
    

Creating Simple Predicates:

Lambdas make it easy to create predicates for filtering and matching operations, especially when working with streams and collections.

Console Output:

Is 4 even? true

Lambda with Supplier

Lambda with Supplier Interface:

The Supplier interface is a functional interface that represents a supplier of results. Lambdas can be used to instantiate suppliers with minimal syntax.


      Supplier<String> supplier = () -> "Hello, World!";
      System.out.println(supplier.get());
    

Generating Values:

Lambdas provide an intuitive way to generate values on demand, which is especially useful in lazy evaluation scenarios.

Console Output:

Hello, World!

Lambda with Consumer

Lambda with Consumer Interface:

The Consumer interface represents an operation that takes a single input argument and returns no result. Lambdas can be used to define consumers easily.


      Consumer<String> greeter = name -> System.out.println("Hello, " + name);
      greeter.accept("Alice");
    

Executing Actions:

Lambdas simplify the execution of specific actions on provided inputs, making it easy to define and execute logic in a functional style.

Console Output:

Hello, Alice

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025