How to use Java Lambda Expressions? [SOLVED]


Written By - Bashir Alam

Lambda expressions are a powerful feature introduced in Java 8 that allows you to write more concise and expressive code. A lambda expression is essentially an anonymous function that can be passed around as a parameter to other methods. It provides a shorthand way of writing anonymous inner classes that implement functional interfaces. In this article, we will discuss how to use the lambda expression in java using various examples.

 

Introduction to Java Lambda Expressions

Lambda expressions are a powerful feature introduced in Java 8 that allows you to write more concise and expressive code. In traditional Java programming, anonymous inner classes were used to implement functional interfaces. However, anonymous inner classes require a lot of boilerplate code, which can make the code difficult to read and understand. Lambda expressions provide a shorthand way of writing anonymous inner classes that implement functional interfaces. They enable you to write code that is more concise, expressive, and readable.

Lambda expressions can be used in many contexts, such as event listeners, collections, and stream processing. They provide a way to pass behavior as a parameter to a method, making it easier to create reusable code.

In essence, a lambda expression is a way to create an anonymous function. It consists of a parameter list, an arrow operator, and a body. The parameter list specifies the parameters that the lambda expression takes, while the arrow operator separates the parameter list from the body. The body contains the code that the lambda expression executes.

 

Basic Syntax of Lambda Expressions

A lambda expression is essentially an anonymous function that can be used to pass behavior as a parameter to a method. The basic syntax of a lambda expression consists of three parts:

  1. Parameter list: This specifies the parameters that the lambda expression takes. The parameters can be of any type, including primitive types, reference types, and arrays.
  2. Arrow operator: This separates the parameter list from the body of the lambda expression. The arrow operator is represented by the symbol " -> ".
  3. Body: This specifies the code that the lambda expression executes. The body can be a single expression or a block of code enclosed in curly braces. If the body is a single expression, the curly braces can be omitted. If the body is a block of code, the return keyword must be used to return a value.

Here's an example of a lambda expression that takes two integers and returns their sum:

(int a, int b) -> { return a + b; }

n this example, the parameter list consists of two integers, and the body returns their sum. The arrow operator separates the parameter list from the body.

You can also simplify the code further by omitting the parameter types since the Java compiler can infer the parameter types from the context:

(a, b) -> { return a + b; }

In this example, the parameter types are omitted, but the parameter names are still specified. The compiler will infer the parameter types from the context in which the lambda expression is used.

Now, we will take various examples of lambda expression in java to understand the concept fully.

 

Functional Interfaces

Functional interfaces are interfaces that have a single abstract method. They are used in Java to enable the creation of Lambda Expressions. In other words, Lambda Expressions can only be used with functional interfaces because they are designed to represent a single unit of behavior, which can be executed in a concise and efficient way.

Functional interfaces are related to Lambda Expressions because they define the signature of the method that can be used in a Lambda Expression. The Lambda Expression can then be used to implement that method without the need for a separate class or interface.

In Java, there are many functional interfaces that are built-in to the language. Here are a few examples:

  1. Runnable: This interface has a single abstract method, run(), which takes no arguments and returns no value. It is used to represent a task that can be executed in a separate thread.
  2. Predicate: This interface has a single abstract method, test(T t), which takes an argument of type T and returns a boolean value. It is used to represent a condition that can be evaluated on an object of type T.
  3. Consumer: This interface has a single abstract method, accept(T t), which takes an argument of type T and returns no value. It is used to represent an operation that can be performed on an object of type T.
  4. Supplier: This interface has a single abstract method, get(), which takes no arguments and returns an object of type T. It is used to represent a function that produces an object of type T.

Functional interfaces provide a way to define the behavior that can be implemented with Lambda Expressions. By using functional interfaces, developers can create more concise and readable code, which is easier to maintain and debug.

 

Lambda Expressions vs. Anonymous Inner Classes

Lambda Expressions and Anonymous Inner Classes are both ways to implement functional interfaces in Java. While they are similar in some ways, there are several differences between the two that are worth noting.

One key difference between Lambda Expressions and Anonymous Inner Classes is their syntax. Lambda Expressions are more concise and easier to read than Anonymous Inner Classes. This is because Lambda Expressions allow you to express the behavior of the interface directly in the code, without the need for a separate class or interface definition. In contrast, Anonymous Inner Classes require more boilerplate code, which can make them more difficult to read and understand.

Another difference between the two is their performance. Lambda Expressions are generally faster and more efficient than Anonymous Inner Classes. This is because Lambda Expressions are implemented using a different bytecode structure than Anonymous Inner Classes, which allows them to be optimized by the Java virtual machine.

Lambda Expressions also have some limitations compared to Anonymous Inner Classes. For example, Lambda Expressions cannot access non-final local variables from the enclosing scope, while Anonymous Inner Classes can. Additionally, Lambda Expressions can only be used with functional interfaces, while Anonymous Inner Classes can be used with any interface that has a single abstract method.

 

Streams and Lambda Expressions

Streams and Lambda Expressions are two important features in Java 8 that work together to make processing collections of data more efficient and easier to read.

Streams are a way to process collections of data in a functional, declarative way. They provide a powerful set of operations that can be performed on collections, such as filtering, mapping, and reducing. With streams, you can express the operations you want to perform on a collection in a concise and readable way, without the need for loops or other control structures.

Lambda Expressions are used in conjunction with streams to provide the behavior that is executed for each element in the collection. A Lambda Expression is a concise way to express a unit of behavior that can be executed in a functional interface. By using Lambda Expressions with streams, you can specify the behavior that should be applied to each element in the collection without the need for a separate loop or control structure.

Here is an example of how Streams and Lambda Expressions can be used together in Java:

List<String> names = Arrays.asList("John", "Jane", "Tom", "Alice");

long count = names.stream()
                  .filter(name -> name.startsWith("J"))
                  .count();

System.out.println("Number of names starting with J: " + count);

In this example, we are using a stream to process a list of names. We are using a Lambda Expression to define the behavior that should be applied to each element in the stream. Specifically, we are filtering the stream to include only names that start with the letter "J", and then counting the number of names that match the filter.

 

Some Practical Examples

Example-1: Sorting a list using lambda expression

One of the common use cases of lambda expressions is sorting a list. Here's an example that sorts a list of strings in ascending order:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SortListExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        Collections.sort(names, (a, b) -> a.compareTo(b));

        System.out.println(names);
    }
}

Output:

[Alice, Bob, Charlie]

The above code is an example of sorting a list of strings in Java using a lambda expression. It first creates a list of strings using the Arrays.asList method, and then sorts the list in ascending order using the Collections.sort method with a lambda expression that compares two strings using the compareTo method. Finally, the sorted list is printed to the console using the System.out.println method.

 

Example-2: Filtering a list in Java using lambda expression

Let us take an example to see how we can use the lambda expression to filter a list in java

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FilterListExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        List<String> filteredNames = names.stream()
                                          .filter(name -> name.startsWith("A"))
                                          .collect(Collectors.toList());

        System.out.println(filteredNames);
    }
}

Output:

[Alice]

In this example, we first create a list of strings using the Arrays.asList method and then create a stream of the elements in the list using the stream method. The filter method is then called on the stream with a lambda expression that returns true if the element starts with the letter "A". The collect method is then called on the stream to collect the filtered elements into a new list. Finally, the filtered list is printed to the console using the System.out.println method. In this case, only the element "Alice" in the original list satisfies the filtering criteria, so it is the only element included in the filtered list.

 

Example-3: Event listeners in Java using lambda expression

Now, we will see how we can use the lambda expression in order to create a listener in Java.

import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Button Example");
        JButton button = new JButton("Click Me");

        button.addActionListener(event -> System.out.println("Button clicked!"));

        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }
}

Output:

butto in java

This example demonstrates the use of event listeners in Java using a lambda expression. It creates a JFrame and a JButton with the text "Click Me". An event listener is added to the button using a lambda expression that simply prints a message to the console when the button is clicked. The button is then added to the frame, which is displayed on the screen. When the button is clicked, the lambda expression is executed and the message "Button clicked!" is printed to the console. Lambda expressions provide a concise and expressive way to define event listeners in Java, which can reduce boilerplate code and make code more readable.

 

Summary

In this article, we explored the topic of lambda expressions in Java. We first introduced the concept of lambda expressions and their benefits and then delved into the basic syntax of lambda expressions in Java. We also provided examples of using lambda expressions for sorting a list and for event listeners.

 

Further Reading

Lambda in Java

 

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment