Java Lambda Expression

Java Lambda Expression

6 mins read229 Views Comment
Updated on Feb 23, 2023 09:10 IST

Java Lambda Expression is a way to define an anonymous function or method that can be passed around as a value. It allows you to write code that is more concise, flexible, and easier to read.

2023_02_MicrosoftTeams-image-306.jpg

Java Lambda Expression was introduced in Java 8, and they are a shorthand notation for creating instances of functional interfaces. A functional interface is an interface that has only one abstract method and can be used to represent a single behavior or action.

Before we dive deeper into the article, let’s go through the list of topics that we will cover in this article:

Table of Contents (TOC)

Syntax

Lambda expressions in Java have the following syntax:

(parameters) -> expression

or

(parameters) -> { statements; }

Here, the “parameters” specify the input arguments for the anonymous function, and the “expression” or “statements” specify the behavior of the function.

 The arrow symbol “-> separates the parameters from the body of the function.

Note: A lambda expression in Java can take any number of arguments.So the above syntax could also look like below:

For zero parameters:

() -> expression

For single parameters:

(parameters) -> expression

For multiple parameters:

(parameter1, parameter2,…,parameterN) -> expression

You can also explore: Practical Guide for HashSet in Java: Example and Use Cases

Use Cases of Java Lambda Expression

Lambda expressions in Java can be used in a variety of contexts where a functional interface is expected, which includes:

  1. Collection operations:

Lambdas can be used with the Stream API to process collections of objects in a functional and concise way.

For example, to filter and map elements in a list:

 
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * 2)
.collect(Collectors.toList());
Copy code
  1. Event handling:

Lambdas can be used to handle events in graphical user interfaces, such as button clicks or mouse movements. 

For example, to handle a button click event:

 
JButton button = new JButton("Click me!");
button.addActionListener(event -> System.out.println("Button clicked!"));
Copy code
  1. Concurrency:

Lambdas can be used to simplify the creation of threads and tasks in multi-threaded applications. 

For example, to create a thread that runs a lambda expression:

 
new Thread(() -> {
// Code to be executed in the thread
}).start();
Copy code
  1. Functional programming:

Lambdas can be used to implement functional programming concepts in Java, such as higher-order functions and currying.

For example, to implement a higher-order function that takes a function and applies it to a value:

 
public static <T, R> R applyFunction(T value, Function<T, R> function) {
return function.apply(value);
}
Copy code
  1. Testing: 

Lambdas can be used to write concise and expressive test cases, especially with frameworks that support fluent interfaces.

For example, to assert that a list contains a certain element:

 
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
assertThat(names).contains("Bob");
Copy code

These are just some of the use cases for lambdas in Java, and they demonstrate the flexibility and power of this feature.

Must explore: Switch Case in Java with Examples

Limitations of Java Lambda Expression

While lambda expressions can be very useful in Java programming, there are some potential drawbacks to using them. Here are some possible drawbacks to consider:

  1. Readability: While lambdas can make code more concise, they can also make it harder to read and understand, especially for developers who are not familiar with functional programming concepts.
  2. Complexity: As the number of lambdas and functional interfaces in a project grows, the code can become more complex and harder to maintain. In some cases, it may be more appropriate to use traditional methods and classes instead of lambdas.
  3. Performance: While lambdas can improve performance in certain scenarios, such as when processing large collections, they can also have a small performance overhead due to the need to create additional objects and perform runtime checks.
  4. Debugging: Because lambdas are anonymous functions, they can be harder to debug than named methods, especially in complex scenarios where multiple lambdas are involved.
  5. Compatibility: Lambdas were introduced in Java 8, so if you are working on a project that needs to be compatible with older versions of Java, you may not be able to use them.
  6. Learning curve: Because lambdas represent a different paradigm of programming, developers who are not familiar with functional programming may need to invest some time to learn the concepts and best practices associated with lambdas.

Overall, the benefits of using lambdas in Java often outweigh the drawbacks, especially in modern, functional-style programming. However, it’s important to be aware of these potential issues and to use lambdas judiciously to ensure that your code remains readable, maintainable, and performant.

You can also explore: Java Basics: Understanding the Fundamentals

Examples of Lambda Expressions in Java

Now that we have made ourselves familiar with the concept of lambda expressions in java, let’s take a look at a couple of examples.

Example 1: Java program that demonstrates the use of lambda expressions to implement a user-defined functional interface.

 
@FunctionalInterface
interface MyFunction {
int apply(int x, int y);
}
public class LambdaExample {
public static void main(String[] args) {
MyFunction addFunction = (x, y) -> x + y;
MyFunction subtractFunction = (x, y) -> x - y;
int result1 = applyFunction(2, 3, addFunction);
int result2 = applyFunction(5, 2, subtractFunction);
System.out.println("Result 1: " + result1);
System.out.println("Result 2: " + result2);
}
public static int applyFunction(int x, int y, MyFunction function) {
return function.apply(x, y);
}
}
Copy code

Output:

  • Result 1: 5
  • Result 2: 3

In this example, we define a functional interface MyFunction that takes two int parameters and returns an int. We mark this interface with the @FunctionalInterface annotation to indicate that it is intended to be used as a functional interface, and therefore, it can be implemented using a lambda expression.

We then define two lambda expressions for addFunction and subtractFunction that implement the MyFunction interface. The lambda expressions add or subtract the input int parameters x and y.

Finally, we define a method applyFunction that takes two int parameters x and y, as well as a functional interface parameter function of type MyFunction. This method applies the given function to the input int parameters x and y and returns the result.

In the main method, we call the applyFunction method twice with different values and different functions and print the results to the console.

Example 2: Java program that demonstrates lambda expressions with zero, one, and two parameters.

 
public class LambdaExample {
public static void main(String[] args) {
// Lambda expression with zero parameters
Runnable zeroParameterLambda = () -> System.out.println("Hello, world!");
zeroParameterLambda.run();
// Lambda expression with one parameter
Consumer<String> oneParameterLambda = (s) -> System.out.println("Hello, " + s + "!");
oneParameterLambda.accept("Alice");
// Lambda expression with two parameters
BiFunction<Integer, Integer, Integer> twoParameterLambda = (x, y) -> x * y;
int result = twoParameterLambda.apply(2, 3);
System.out.println("Result: " + result);
}
}
Copy code

Output:

  • Hello, world!
  • Hello, Alice!
  • Result: 6

In this example, we define three lambda expressions:

  1. A lambda expression with zero parameters, which is used to define a Runnable that prints “Hello, world!” to the console when run.
  2. A lambda expression with one parameter, which is used to define a Consumer<String> that prints “Hello, ” followed by the input String to the console when accept is called.
  3. A lambda expression with two parameters, which is used to define a BiFunction<Integer, Integer, Integer> that multiplies its two input int parameters and returns the result.

In the main method, we create instances of each of these lambda expressions and call their respective methods to run them.

Example 3: Java program that demonstrates how to use lambda expressions with a foreach loop.

 
import java.util.ArrayList;
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");
names.forEach(name -> System.out.println("Hello, " + name + "!"));
}
}
Copy code

Output:

  • Hello, Alice!
  • Hello, Bob!
  • Hello, Charlie!

In this example, we create a List of String objects called names and add three names to it. We then call the forEach method on the names list and pass in a lambda expression as the argument.

The lambda expression takes a single String parameter called name, and prints “Hello, ” followed by the value of name, and an exclamation mark, to the console.

You can also explore: Wrapper Class in Java 

Conclusion

In this article we have managed to cover the following concepts:

  • What are Lambda expressions?
  • Use case of Java Lambda Expression
  • Possible limitations of Java Lambda Expression
  • Example uses of Lambda Expression in Java
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio