Skip to content

Sihendri

Little Note about technology

Menu
  • Home
  • About
  • Contact
Menu

Lambda Expressions in Java

Posted on February 24, 2023 by sihendri

Lambda expressions were introduced in Java 8 as a new way to write code in a functional style. Lambda expressions provide a way to write concise, easy-to-read code that can be treated as data, passed as arguments to methods, and executed later.

A lambda expression is an anonymous function that can be defined inline and passed around as a variable. Lambda expressions can be thought of as a shorthand way to write anonymous classes that implement a functional interface, which is an interface with a single abstract method. The syntax for a lambda expression is as follows:

(parameter list) -> { expression body }

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

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

Lambda expressions can also be written in a more concise form when the parameter types and return type can be inferred:

(a, b) -> a + b

Lambda expressions can be used in a variety of contexts in Java, including:

  1. Collections: Lambda expressions can be used to iterate over collections, filter elements, and perform operations on each element.

For example, here’s a lambda expression that uses the forEach() method to print each element in a list:

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));
  1. Streams: Streams are a powerful new feature in Java 8 that allow you to perform functional-style operations on collections. Lambda expressions can be used to define the operations to perform on each element in a stream.

Here’s an example of a lambda expression that uses a stream to filter even numbers from a list, and then squares each number:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

List<Integer> result = numbers.stream() .filter(n -> n % 2 == 0) .map(n -> n * n) .collect(Collectors.toList());

 

  1. Runnable and Callable: Lambda expressions can be used to define the code to be executed by a Runnable or Callable interface.

For example, here’s a lambda expression that defines a Runnable that prints “Hello, world!” to the console:

Runnable r = () -> System.out.println("Hello, world!");

 

  1. Comparator: Lambda expressions can be used to define the comparison logic for a Comparator interface.

For example, here’s a lambda expression that defines a Comparator that compares two Person objects by their age:

Comparator<Person> byAge = (p1, p2) -> Integer.compare(p1.getAge(), p2.getAge());

Lambda expressions have become a popular feature in Java because they allow developers to write code that is more concise, expressive, and easier to read. However, it’s important to use lambda expressions judiciously and understand their limitations, such as the inability to reference non-final variables from the enclosing scope.

In summary, lambda expressions are a powerful new feature in Java that provide a way to write functional-style code in a concise and expressive way. Lambda expressions can be used in a variety of contexts, including collections, streams, Runnable and Callable interfaces, and Comparator interfaces. When used correctly, lambda expressions can help improve the readability and maintainability of your code.

Share on Social Media
twitter facebook linkedinwhatsapptelegram

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • Docker
  • java
  • News
  • Oracle
  • sQL
  • Uncategorized
  • weblogic
  • Windows
  • Wordpress

Latest Post

  • SGA and PGA in Oracle Database Configuration
  • Rumor: NVIDIA GeForce RTX 4070 to be Released on April 13, 2023
  • ASUS Zenfone 10 Leak Reveals It Might No Longer Be Compact
  • How To Substring Until First Space in Oracle String
  • TikTok set daily screen time limit for under 18s to 60 minutes
©2023 Sihendri | Built using WordPress and Responsive Blogily theme by Superb