Java8 New Feature: Summary of lambda Expressions
- 2021-10-13 07:35:15
- OfStack
1. Basic syntax for Lambda expressions
Basic syntax for Lambda expressions: A new operator "-" is introduced in Java 8 > "This operator is called the arrow operator or the Lambda operator. The arrow operator splits the Lambda expression into two parts:
Left: Parameter list of Lambda expression Right side: the function to be performed in the Lambda expression, that is, the Lambda bodySyntax Format 1: No parameters, no return value
() -> System.out.println("Hello Lambda!");
Syntax Format 2: Has 1 parameter and no return value
(x) -> System.out.println(x)
Syntax Format 3: If there is only one parameter, the parentheses can be omitted
x -> System.out.println(x)
Syntax Format 4: There are more than two parameters, return values, and multiple statements in the body of Lambda
Comparator<Integer> com = (x, y) -> {
System.out.println(" Functional interface ");
return Integer.compare(x, y);
};
Syntax Format 5: If there is only one statement in the body of Lambda, return and curly braces can be omitted
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
Syntax Format 6: The data type of the parameter list of the Lambda expression can be omitted, because the JVM compiler infers the data type through context, that is, "type inference"
(Integer x, Integer y) -> Integer.compare(x, y);
2. Functional interface
Lambda expressions require "functional interface" support
Functional interface: An interface with only one abstract method in the interface, which is called functional interface. You can use the annotation @ FunctionalInterface modification to check whether it is a functional interface.
Four Core Functional Interfaces Built in Java8
Consumer<T> : Consumer interface
void accept(T t);
Supplier<T> : Supply interface
T get();
Function<T, R> : Functional interface
R apply(T t);
Predicate<T> : Assertive interface
boolean test(T t);
3. Four built-in core functional interface usage examples
package com.lyz.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.junit.Test;
/*
* @author liuyazhuang
* @version 1.0.0
* @date 2018/8/19 15:02
* @description Java8 Built-in 4 Large core functional interface
*
* Consumer<T> : Consumer interface
* void accept(T t);
*
* Supplier<T> : Supply interface
* T get();
*
* Function<T, R> : Functional interface
* R apply(T t);
*
* Predicate<T> : Assertive interface
* boolean test(T t);
*
*/
public class TestLambda {
//Predicate<T> Assertive interface:
@Test
public void test4(){
List<String> list = Arrays.asList("Hello", "world", "Lambda", "www", "ok");
List<String> strList = filterStr(list, (s) -> s.length() > 3);
for (String str : strList) {
System.out.println(str);
}
}
// Requirement: Put the string that meets the condition into the collection
public List<String> filterStr(List<String> list, Predicate<String> pre){
List<String> strList = new ArrayList<>();
for (String str : list) {
if(pre.test(str)){
strList.add(str);
}
}
return strList;
}
//Function<T, R> Functional interface:
@Test
public void test3(){
String newStr = strHandler("\t\t\t My name is Liu Yazhuang ", (str) -> str.trim());
System.out.println(newStr);
String subStr = strHandler(" My name is Liu Yazhuang ", (str) -> str.substring(2, 5));
System.out.println(subStr);
}
// Requirements: Used to process strings
public String strHandler(String str, Function<String, String> fun){
return fun.apply(str);
}
//Supplier<T> Supply interface :
@Test
public void test2(){
List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));
for (Integer num : numList) {
System.out.println(num);
}
}
// Requirement: Generate a specified number of integers and put them into the collection
public List<Integer> getNumList(int num, Supplier<Integer> sup){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
Integer n = sup.get();
list.add(n);
}
return list;
}
//Consumer<T> Consumer interface :
@Test
public void test1(){
happy(10000, (m) -> System.out.println(" Each consumption: " + m + " Yuan "));
}
public void happy(double money, Consumer<Double> con){
con.accept(money);
}
}