Home Java 8 Lamda Expressions
Post
Cancel

Java 8 Lamda Expressions

Lambda Expressions

Java 8 introduced a new feature called Lambda expressions, which has been a significant step forward for the Java programming language. Lambda expressions are a way to write concise and readable code by enabling functional programming in Java. In this article, we’ll explore what Lambda expressions are, how they work, and why they are essential in modern Java programming.

What is a Lambda expression?

A Lambda expression is a concise way to represent an anonymous function that can be passed around as an argument to other functions. The syntax for a Lambda expression is as follows:

1
(parameter_list) -> expression

The parameter list represents the input parameters to the function, and the expression represents the output of the function.

Lambda expressions are particularly useful in functional programming because they allow functions to be passed around as objects. This allows for a more flexible and modular approach to programming, as functions can be reused in different contexts.

Examples

Example 1: Sorting a list of strings using a Lambda expression

1
2
3
List<String> names = Arrays.asList("Ram", "John", "Adam", "Eve");
Collections.sort(names, (a, b) -> a.compareTo(b));
System.out.println(names);

In this example, we create a list of strings and sort them using a Lambda expression. The Lambda expression (a, b) -> a.compareTo(b) compares two strings a and b and returns a negative integer, zero, or a positive integer depending on whether a is less than, equal to, or greater than b. We pass this Lambda expression as the second argument to the Collections.sort method, which sorts the list in ascending order.

Example 2: Filtering even number in a list of integers using a Lambda expression

1
2
3
4
5
6
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> evenNumbers = numbers.stream()
                                    .filter(n -> n % 2 == 0)
                                    .collect(Collectors.toList());
System.out.println(evenNumbers);

n this example, we create a list of integers and use a Lambda expression to filter out even numbers. We use the stream() method to create a stream of integers from the list, and then use the filter method to create a new stream that contains only even numbers. Finally, we use the collect method to collect the even numbers into a new list. The Lambda expression n -> n % 2 == 0 checks if the integer n is even by checking if it is divisible by 2 with no remainder.

Example 3: Mapping a list of strings to their lengths using a Lambda expression

1
2
3
4
5
6
List<String> names = Arrays.asList("Ram", "John", "Adam", "Eve");
List<Integer> nameLengths = names.stream()
                                 .map(name -> name.length())
                                 .collect(Collectors.toList());
System.out.println(nameLengths);

In this example, we create a list of strings and use a Lambda expression to map each string to its length. We use the stream() method to create a stream of strings from the list, and then use the map method to create a new stream that contains the length of each string. Finally, we use the collect method to collect the length of each string into a new list. The Lambda expression name -> name.length() returns the length of the string name.

Example 4: Reducing a list of integers using a Lambda expression

1
2
3
4
5
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
int sum = numbers.stream()
                 .reduce(0, (a, b) -> a + b);
System.out.println(sum);

In this example, we create a list of integers and use a Lambda expression to reduce the list to a single value, which is the sum of all the integers in the list. We use the stream() method to create a stream of integers from the list, and then use the reduce method to reduce the stream to a single value. The reduce method takes an initial value of 0 and a Lambda expression (a, b) -> a + b that adds each integer in the stream to the accumulator a and returns the sum.

Example 5: Creating a thread using a Lambda expression

1
2
3
4
new Thread(() -> {
    System.out.println("Hello, a am a new thread!");
}).start();

In this example, we create a new thread using a Lambda expression. We create a new Thread object and pass a Lambda expression () -> { System.out.println(“Hello from a thread!”); } as the thread’s task. The Lambda expression simply prints a message to the console. We then start the thread by calling the start method on the Thread object. The Lambda expression allows us to create a simple task that can be executed in a separate thread without defining a new class or implementing an interface.

Benefits of Lambda expressions

Lambda expressions provide several benefits to Java developers, including:

  • Concise code: Lambda expressions allow for more concise and readable code, which makes it easier to understand and maintain.

  • Functional programming: Lambda expressions enable functional programming, which provides a more flexible and modular approach to programming.

  • Parallel processing: Lambda expressions can be used to process data in parallel, which can significantly improve performance.

  • Reduced memory usage: Lambda expressions do not require the creation of objects, which reduces memory usage and improves performance.

Conclusion

Lambda expressions are a significant addition to the Java programming language, providing a more concise and readable way to write code. They enable functional programming, which provides a more flexible and modular approach to programming. In addition, they can be used to process data in parallel, which can significantly improve performance. Overall, Lambda expressions are an essential feature in modern Java programming and should be a part of every Java developer’s toolkit

This post is licensed under CC BY 4.0 by the author.