Common summary of basic grammar for Lambda development

Lambda

​ a) Lambda is an anonymous function, passable code (concise), using Lambda expressions can solve anonymous inner classes

​ b) "Type inference" examples are as follows:

			Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
			
			List<Integer> list = new ArrayList<Integer>();
			List<Integer> list = new ArrayList<>();

​ c) Functional interface (@FunctionalInterface): An interface with only one abstract method in the interface

​ d) Java 8 built-in four core functional interfaces:

​ 1) Consumer<T>: Consumer interface void accept(T t);

​ 2) Supplier<T>: Supply interface T get();

​ 3) Function<T, R>: functional interface R apply(T t);

​ 4) Predicate<T>: assertion type interface boolean test(T t);


e) Method Three reference syntax

​ 1) Object:: instance method name

​ 2) Class:: static method name

​ 3) Class:: instance method name

​ Note:

​ 1) The parameter list and return value type of the calling method in the lambda body should be consistent with the function list and return value type of the abstract method in the functional interface!

​ 2) If the first parameter in the lambda parameter list is the caller of the instance method, and the second parameter is the parameter of the instance method, you can use ClassName::method

​ f) Constructor reference

​ Format: ClassName::new

Note : This parameter list constructor needs to call a list of parameters to be consistent with the functional method in the abstract interface!

​ g) Array reference

​ Format: Type[]:: new

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/106964719