Lambda Expressions | Benefits of Lambda expressions | Examples of Lambda expressions

Lambda Expressions | Benefits of Lambda expressions | Examples of Lambda expressions in Java

Lambda Expressions in Java

Lambda expressions are a feature introduced in Java 8 that allow you to write more concise and functional-style code. They provide a way to represent anonymous functions and are particularly useful when working with functional interfaces.

Why Lambda Expressions are Required:

  • Lambda expressions eliminate the need for writing lengthy anonymous inner classes, making the code more readable and less cluttered.
  • They promote functional programming concepts and enable developers to write more expressive and concise code.
  • Lambda expressions facilitate the use of functional interfaces, which are a key component of Java's functional programming support.

How to Use Lambda Expressions:

  • Lambda expressions have a compact syntax: (parameters) -> expression or (parameters) -> { statements }.
  • They can be used wherever a functional interface is expected, such as in method arguments or assignments.
  • Lambda expressions can be used to define behavior inline, making the code more concise and readable.

Where to Use Lambda Expressions:

  • Lambda expressions are commonly used with functional interfaces like java.util.function.Predicate, java.util.function.Consumer, and java.util.function.Function.
  • They are especially useful in collection processing operations like filtering, mapping, and reducing using the Stream API.
  • Lambda expressions can be used in event handling, parallel processing, and other scenarios where concise and expressive code is desired.

Lambda Expressions Examples

Example 1: Runnable Interface

    
Thread thread = new Thread(() -> {
  System.out.println("Hello from a Lambda expression!");
});
thread.start();
    
  

In this example, a Lambda expression is used as the implementation of the Runnable interface. It creates a new thread and prints a message using the Lambda expression.

Example 2: Filtering a List

    
List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");
fruits.stream()
      .filter(fruit -> fruit.startsWith("A"))
      .forEach(System.out::println);
    
  

This example demonstrates the use of a Lambda expression to filter a list of fruits. It uses the filter method of the Stream API to select fruits starting with the letter "A" and prints them using the forEach method.

Example 3: Mapping a List

    
List<String> names = Arrays.asList("John", "Jane", "Tom");
List<Integer> nameLengths = names.stream()
                                   .map(name -> name.length())
                                   .collect(Collectors.toList());
    
  

In this example, a Lambda expression is used to map the length of names in a list to a new list of integers. It uses the map method of the Stream API to transform each name into its length.

Example 4: Comparator for Sorting

    
List<String> countries = Arrays.asList("USA", "India", "Japan");
Collections.sort(countries, (country1, country2) -> country1.compareTo(country2));
    
  

This example demonstrates the use of a Lambda expression as a Comparator to sort a list of countries alphabetically. It uses the sort method of the Collections class and compares the countries based on their names.

Example 5: ActionListener for Button

    
JButton button = new JButton("Click Me");
button.addActionListener(event -> System.out.println("Button clicked!"));
    
  

In this example, a Lambda expression is used as an ActionListener for a button. When the button is clicked, the Lambda expression prints a message to the console.

Post a Comment

Previous Post Next Post