lambda expressions are not recommended to be used frequently. If they are used frequently, your code will be very concise but difficult to understand, and it will be difficult to maintain later. Colleagues want to beat people after reading the series.
explain:
Lambda is a new operator "->" introduced in Java 8, which becomes an arrow operator or lambda operator.
Left: list of parameters for lambda expression.
Right: the function to be performed in the lambda expression, that is, the lambda body.
Depending on the functional interface, lambda expression is the implementation of the interface.
characteristic:
1.lambda makes sentence patterns more concise.
If you don't talk much, say chestnuts:
@Test void contextLoads() { //lambda expression not used Runnable runnable1 = new Runnable() { @Override public void run() { System.out.println("404"); } }; //Using lambda expressions Runnable runnable2=()->System.out.println("404"); }
You can clearly see the difference between using lambda expressions and not using them.
2. Variables in lambda
Chestnuts:
int s=5; Runnable runnable2=()->System.out.println(s++);
This sentence will report an error because when calling a local variable with lambda, it will be automatically decorated with final
Although constants cannot be changed, we can define numbers and change the values in the array
int[] ss=new int [1]; ss[0]=1; Runnable runnable2=()->{ ss[0]++; System.out.println( ); };
So you don't report mistakes
3. There is a parameter value
Consumer<String> consumer = (x)->{System.out.println(x);}; consumer.accept("s886");
This code takes the parameter obtained by accept as x and then outputs it.
4. There is only one parameter, omitting parentheses
Consumer<String> consumer = x->{System.out.println(x);}; consumer.accept("ssssss");
When there is only one parameter, () can be omitted, and the purpose of lambda expression is to save.
5. There are more than two parameters, return values and multiple statements
Comparator<Integer> consumer1 = (x, y)->{ System.out.println("ISO-8859-1"); return Integer.compare(x,y); };
The parentheses of the parameter cannot be omitted. return is required.
6. When there is only one statement in lambda
Comparator<Integer> consumer2 = (x, y)-> Integer.compare(x,y);
Both {} and return can be omitted
Four built-in functions:
lambda expressions rely on four common built-in functions.
type | class | Abstract method |
Consumer interface | Consumer<T> | void accept(T t) |
Supply type interface | Supplier<T> | T get(); |
Functional interface | Function<T,R> | R apply(T t) |
Assertive interface | Predicate<T> | boolean test(T t) |
Custom function interface:
1. Digital type
public static List<Integer> Number (int n, Supplier<Integer> s){ List<Integer> list =new ArrayList<>(); for(int i=0;i<n;i++){ list.add(s.get()); } return list; } public static void main(String[] args) { List<Integer> num = Number(4,()->(int)(Math.random()*100)); num.forEach(System.out::println); }
The above is the method we defined. The first parameter n is the number of numbers we need to add, and the second parameter is a supply function interface.
(how to generate quickly) List<Integer> list =new ArrayList<>();
2. String type
public static String Str(String str, Function<String ,String>fun){ return fun.apply(str); } public static void main(String[] args) { String re = Str("404+",(x)->x+"505"); System.out.println(re); }
The first parameter is a string, and the second parameter is a functional interface. When calling, we assign x + "string" to re, and then output it.
Method reference:
1. Object:: instantiation method name
@Test public void test1(){ Consumer<String> con = System.out::println; con.accept("asdasd"); }
2. Class:: static method name
@Test public void test2(){ Comparator<Integer> com1 =(x,y)->Integer.compare(x,y); Comparator<Integer> com2 = Integer::compare; }
The two methods are equivalent. The second method is equivalent to directly calling the static method compare in Integer.
3. Class: instance method name
@Test public void test3(){ BiPredicate<String,String> com1 =(x, y)->x.equals(y); BiPredicate<String,String> com2 = String::equals; }
The two methods are equivalent, but in the second method, the first parameter is the caller of the method and the second parameter is the parameter of the method.