1. What is a Lambda expression
Lambda expression is a feature introduced by Java 8. It allows you to write more concise code in the form of anonymous functions, so it can also be understood as it and specific.Anonymous functionsIt is the same meaning, but the writing method is different and it is more concise (of course, if you are not used to it, you may find it complicated).
The basic syntax of Lambda expressions is(parameters) -> expression
,in->
Indicates mapping parameters to expressions.
Lambda expressions can be used to implement functional interfaces, i.e.Contains only one abstract methodofinterface。
The above red font illustrates the difference between Lambda expression and ordinary anonymous functions. The conditions required are relatively strict and are not suitable for simplifying all anonymous functions.
2. The difference between Lambda expression and anonymous function
The above has been explained that Lambda expressions, like anonymous functions, can implement functional interfaces, but can only contain the only abstract method, and are interface interface classes.
public class LambdaTest { public static void main(String[] args) { checkId1("1", User::getUserIdStatic); //Use the static method of the reference class, the resulting id is related to the static value of the User class User user = new User("2"); checkId1("1", user::getUserId); //Use the reference instance object method, the obtained id is related to the user instance checkId1("1", ()-> ()); //Use normal Lambda expressions (simplified {} and return), and add {} after -> to write methods to implement contents normally checkId2("1", new ICommonFun(){ //Use anonymous functions, because the number of abstract methods of this interface is greater than 1 @Override public String getId() { return (); } @Override public void setId(String id) { } }); } public static void checkId1(String id, ILambdaFun iLambdaFun){ if ((())){ ("Identity of id"); }else{ ("Is not equal"); } } public static void checkId2(String id, ICommonFun iCommonFun){ if ((())){ ("Identity of id"); }else{ ("Is not equal"); } } interface ILambdaFun { String getId(); // The number of method parameters is 0, so the lambda expression is ()->{} } interface ICommonFun { String getId(); void setId(String id); //This method is too much, so it is impossible to use Lambda expressions to implement this interface. Only by removing or changing to non-abstract methods can you use Lambda expressions to implement this interface. } static class User { private String id; private static String idStatic = "1"; User(String id){ = id; } private String getUserId () { return id; } private static String getUserIdStatic () { return idStatic; } } } // Run results//The id is equal//Is not equal//Is not equal//idNot equal
3. Four methods of Lambda expression references
In the above code example, when using Lambda expressions, two method references are used, and Lambda normal expressions are used. There are 4 method references in Lambda expressions:
1. Static method reference (the above code example has been used)
2. Reference of the example method (the above code example has been used)
3. Constructor method reference
4. Special method reference
The first: Static method reference
Quote syntax: class name:: method name, such as User::getIdStatic()
Notes:
What is the abstract method parameter list in the functional interface (interface class), and the referenced method parameter list must be the same;
The abstract method of the interface has no return value, and the referenced method returns value is optional;
The abstract method of the interface has a return value, and the referenced method must have a return value of the same type.
The second type: Instance method reference
Quote syntax: object name::non-static method name, such as: user::getId()
Notes:
What is the abstract method parameter list in the functional interface (interface class), and the referenced method parameter list must be the same;
The abstract method of the interface has no return value, and the referenced method returns value is optional;
The abstract method of the interface has a return value, and the referenced method must have a return value of the same type.
The third type: Constructor method reference
Quote syntax: class name::new, such as: String::new
Notes:
What is the abstract method parameter list in a functional interface (interface class), and the referenced constructor parameter list should be the same.
Fourth: Special method reference
Quote syntax: class name:: instance method name, such as User::getId()
Notes:
In abstract methods, the first parameter can be used asQuoted instance method(such as getId()) caller (such as User instance object) can be simplified. Therefore, this abstract method must have at least one parameter, and the first parameter is an object that can call the instance method; except for the parameter list composed of the remaining parameters of the first parameter, there must be a corresponding non-static instance method in the object class represented by the first parameter. (For example, the abstract method is void setObject(User user,String v1), then the User class must have fun(String v1) method, and the method return value is the same as the first and second requirements.)
public class LambdaTest { public static void main(String[] args) { checkId1("1", User::getUserId); // Looks like referenced static methods } public static void checkId1(String id, ILambdaFun iLambdaFun){ if (((new User("1")))){ // When using abstract methods of function instances, you need to pass into the instance object according to the parameters ("Identity of id"); }else{ ("Is not equal"); } } interface ILambdaFun { Object getObject(User user); // There is only one parameter in the abstract method } static class User { private String id; private static String idStatic = "1"; User(String id){ = id; } private String getUserId () { return id; } private static String getUserIdStatic () { return idStatic; } } }
4. Expand
Some interfaces in Java that can use Lambda expressions
-
Consumer<T>
: Accept an input parameter and does not return the result. -
Supplier<T>
: No parameters are accepted, a result is returned. -
Function<T, R>
: Accepts a parameter and returns a result. -
Predicate<T>
: Accepts a parameter and returns a boolean value.
public class LambdaTest { public static void main(String[] args) { List<User> userList = new ArrayList<>(); User user1 = new User("1"); (user1); (new User("2")); HashMap<String,User> map = listToMap(userList, User::getUserId); //You need other attribute values as the key of the map, just change the reference method (("1")); (("2")); } public static HashMap<String, User> listToMap(List<User> userList, Function<User, String> function){ HashMap<String, User> userMap = new HashMap<>(); ((item)->{ ((item), item); }); return userMap; } public static class User { private String id; private static String idStatic = "1"; User(String id){ = id; } private String getUserId () { return id; } private static String getUserIdStatic () { return idStatic; } @Override public String toString() { return "User{id:"+id+"}"; } } } // Run results//User{id:1} //User{id:2}
This is the end of this article about the understanding and use of Lambda expressions in Java 8. For more related Java 8 Lambda expression content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!