Home  >  Article  >  Java  >  How to use Lamdba expressions in java

How to use Lamdba expressions in java

WBOY
WBOYforward
2023-04-27 15:22:161076browse

1. If Lamdba only requires one parameter, the parentheses can be omitted

Consumer<String> con = str -> {System.out.println(str)}

2. Lamdba requires one parameter, but there is no return value

Consumer<String> con = (String str) -> {System.out.println(str)}

3. The data type can be omitted.

Because it can be inferred by the compiler, it is called type inference

Consumer<String> con = (str) -> {System.out.println(str)}

4. Lamdba requires more than two parameters.

Multiple execution statements, and can have return values

Comparator<Integer>com = (o1,o1) -> {
Syste.out.println("Lamdba表达式使用");
    return Integer.compare(o1,o2);
}

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

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete