This is the 25th day of my participation in the August Genwen Challenge.More challenges in August
Lambda expression type inference
In the lambda method reference article juejin.cn/post/699999… Lambda is an anonymous method that returns an object that implements the specified interface, so you need to specify which interface is implemented or you’ll get an error. This is type inference
Type inference comes in a number of ways
@FunctionalInterface interface IMath{ int add(int x,int y); } public class TypeDemo {public static void main(String[] args) {IMath lambda = (x,y)->x+y; IMath[]lambdas = {(x,y)->x+y}; Object lambda2 = (IMath)(x,y)->x+y; IMath createLambda =createLambda(); TypeDemo demo = new TypeDemo(); demo.test((x, y) -> x+y); } public void test(IMath math){ } }Copy the code
That’s the way it works in general
Create a new interface IMath2 under IMath
@FunctionalInterface
interface IMath2{
int sub(int x,int y);
}
Copy the code
Let’s reload test
public void test(IMath math){
}
public void test(IMath2 math){
}
Copy the code
There is a problem
Overloading test raises does not specify which one it is, so we need to specify the type when an error is reported
Demo.test ((IMath2) (x, y) -> x+y);Copy the code
Lambda expression variable references,
Since lambda is an anonymous class, the reference process is the same as for anonymous classes. Look at the code
Public class VarDemo {public static void main(String[] args) {String STR = "This String cannot be modified "; str = "44944"; Consumer<String> consumer = s-> System.out.println(s+str); consumer.accept("1211"); }}Copy the code
Why can’t this STR be modified? Those of you who have used previous versions of Java 8 will know that final is used to refer to objects outside anonymous classes, but Java 8 does that by default, so it doesn’t matter if you don’t write final here
Then we don’t understand. Why do we add final
First of all, do Java pass parameters by value or by reference? The answer is pass value. Let’s look at another piece of code
List<String> list = new ArrayList<>(); Consumer2 = s-> system.out.println (s+list); consumer2 = s-> system.out.println (s+list); consumer2.accept("1234");Copy the code
When the code is written this way, it makes it easier to explain why it’s A value reference, and it makes it unmodifiable. When the list above is called at C, A -> B -> C if the value changes, so A -> D, but because it’s A value reference, Cause B ->C to call a list with a different value.
If it’s A quote then it’s C -> B -> A, it doesn’t matter how you change A, right
Creasy of cascade expressions
What is a cascading expression?
A cascading expression is an expression of multiple arrows x->y -> x+ y
What does the above expression mean? Lambda expressions have inputs on the left and outputs on the right. So let’s look at the first one — >, I put in x and I put out y — >x+y; So it’s a Function and the second one — >, it puts in y and it puts out x plus y, so it’s also a Function and then it’s an x plus y, so it could be a number or a string, so let’s think of it as a number
public static void main(String[] args) {
Function<Integer,Function<Integer,Integer>>function = x->y ->x+y;
System.out.println(function.apply(2).apply(3));
}
Copy the code
To convert a function with multiple arguments to a function with only one argument
Before currization
Function<Integer,Function<Integer,Function<Integer,Integer>>> fun2 = x->y->z -> x+y+z;
System.out.println(fun2.apply(2).apply(3).apply(4));
Copy the code
Wouldn’t more and more functions mean longer and longer
After currization…
Function f = fun2; for (int i=0; i<nums.length; i++){ if (f instanceof Function){ Object obj = f.apply(nums[i]); if (obj instanceof Function){ f = (Function) obj; }else {system.out.println (" end of call: result "+obj); }}}Copy the code
I’m calling a function, and I’m still returning the function.