Home>Article>Java> Learn to use lambda expressions in Java

Learn to use lambda expressions in Java

王林
王林 Original
2023-06-16 08:11:55 1070browse

Nowadays, Java has become one of the most widely used programming languages. In Java 8, Lambda expressions were introduced, making using Java more concise, efficient, and flexible. In this article, we will introduce the concept, usage and examples of Lambda expressions to help beginners quickly master this feature.

1. The concept of Lambda expression

Lambda expression is a convenient anonymous function that can be passed to a method. It represents a passed block of code and can be used as a cleaner and more flexible way to write code.

Lambda expression consists of three parts:

  1. Parameter list: the data input into the Lambda expression;
  2. Arrow (->): split Symbol, indicating the relationship between the passed parameters and the Lambda expression;
  3. Code block: the code to be executed in the Lambda expression.

2. The use of Lambda expressions

If you have used Java's functional interface, then Lambda expressions are very easy to master. Java's functional interface refers to an interface with only one abstract method. Lambda expressions are often used with these functional interfaces to simplify and enhance the readability and maintainability of code.

You need to pay attention to the following points when using Lambda expressions:

  1. Definition of functional interface: The target type of Lambda expression should be a functional interface, that is, one with only one abstract method interface. For example, both Function and Predicate in Java 8 are functional interfaces.
  2. The meaning of the arrow (->): indicates the relationship between the passed parameters and the Lambda expression.
  3. Definition of code block: The code block in a Lambda expression can be an expression or a statement block.

3. Examples of Lambda expressions

The following are some examples of Lambda expressions for you to better understand:

  1. Express with Lambda Formula to traverse the collection

Listf7e83be87db5cd2d9a8a0b8117b38cd4 list = Arrays.asList("Apple", "Orange", "Banana");
list.forEach(str -> System.out. println(str));

  1. Use Lambda expression to sort the collection

Listc0f559cc8d56b43654fcbe4aa9df7b4a list = Arrays.asList(5, 3, 1, 2, 4);
Collections.sort(list, (a, b) -> a.compareTo(b));
System.out.println(list);

  1. Use Lambda expression filters the collection

Listc0f559cc8d56b43654fcbe4aa9df7b4a list = Arrays.asList(1, 2, 3, 4, 5);
Listc0f559cc8d56b43654fcbe4aa9df7b4a newList = list.stream( ).filter(num -> num % 2 == 0).collect(Collectors.toList());
System.out.println(newList);

  1. Use Lambda expressions With functional interface

public interface Function469df1b70b2914a3841e0404ec3e3d5d {

R apply(T t);

}

Function754751e56704d0427ba7371efce67d95 intToString = num -> String. valueOf(num);
String str = intToString.apply(10);
System.out.println(str);

4. Summary

Lambda expression is Java An important feature in 8. It can make the code more concise, flexible and readable. This article introduces the concept, usage and examples of Lambda expressions, hoping to be helpful to beginners. Mastering the use of Lambda expressions will help you easily write more efficient, streamlined, and maintainable Java code.

The above is the detailed content of Learn to use lambda expressions in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn