Java: methods, constructors, array references

1. Method reference

When the operation to be passed to the Lambda body, there is already a way to implement it, and you can use method references!

Method reference can be seen as a deep expression of Lambda expressions. In other words, a method reference is a Lambda expression, which is an instance of a functional interface. Pointing to a method by the name of the method can be regarded as a syntactic sugar of Lambda expression.

Requirement: The parameter list and return value type of the abstract method that implements the interface must be consistent with the parameter list and return value type of the method referenced by the method!

Format: Use the operator "::" to separate the class (or object) from the method name.

There are three main usage scenarios as follows:

  • Object:: instance method name
  • Class:: static method name
  • Class:: instance method name

(1) Object reference:: instance method

public class FunCall_01 {
    
    
	public static void main(String[] args) {
    
    
		Integer i1 = new Integer(12356);
		
		// 常规lambda表达式
		// 使用供应商接口,不需要参数
		// 返回字符串
		Supplier<String> su = () -> i1.toString();
		System.out.println(su.get());
		
		// 方法引用的写法
		Supplier<String> su1 = i1::toString;
		System.out.println(su1.get());

	}
}

(2) Class name:: static method name

public class FunCall_02 {
    
    
	public static void main(String[] args) {
    
    
		// 常规的lambda写法
		// 前两个是 参数类型,第三个是返回值类型
		BiFunction<Integer, Integer, Integer> bif = (x,y) -> Integer.max(x, y);
		int apply = bif.apply(10, 11);
		System.out.println(apply);
		
		// 方法引用的写法
		BiFunction<Integer, Integer, Integer> bif1 = Integer::max;
		int apply1 = bif1.apply(19, 2);
		System.out.println(apply1);
	}
}

(3) Class name:: instance method

public class FunCall_03 {
    
    
	public static void main(String[] args) {
    
    
		// 传统的lambda写法
		// 两个字符串类型参数 x,y, 返回boolean 
		BiPredicate<String, String> predicate = (x,y) -> x.equals(y);
		System.out.println(predicate.test("a", "c"));
		
		// 方法引用写法
		BiPredicate<String, String> predicate1 = String::equals;
		System.out.println(predicate1.test("a", "a"));
	}
}

2 Constructor reference:

Format: ClassName::new

Combined with the functional interface, it is automatically compatible with the methods in the functional interface.
The constructor reference can be assigned to the defined method, and the constructor parameter list is required to be consistent with the parameter list of the abstract method in the interface! And the return value of the method is the object of the corresponding class of the constructor.

public static void main(String[] args) {
    
    
		// 无参构造器
		// 常规的lambda写法
		// 供应商接口,没有参数,供应 Object 类型
		Supplier<Object> objSup = ()-> new Object();
		System.out.println(objSup.get());
		
		// 方法引用
		Supplier<Object> objSup1 = Object::new;
		System.out.println(objSup1.get());
		
		// 有参构造器
		// 常规的lambda写法
		// 参数是字符串,返回值是整数
		Function<String, Integer> func = x -> new Integer(x);
		System.out.println(func.apply("123"));
		
		// 方法引用写法
		Function<String, Integer> func1 = Integer::new;
		System.out.println(func1.apply("54"));
	}

3 Array reference

For example, define an array of type Integer with a length of n.

public static void main(String[] args) {
    
    
	// lambda写法
	// 参数是数组长度,返回值是 Integer类型的数组
	Function<Integer, Integer[]> fun = n -> new Integer[n];
	//传入 10 ,创建长度为10的数组
	Integer[] arr = fun.apply(10);
	System.out.println(arr.length);//10

	// 数组引用写法
	Function<Integer, Integer[]> fun1 = Integer[]::new;
	Integer[] arr1 = fun1.apply(2);
	System.out.println(arr1.length);
	
}

Guess you like

Origin blog.csdn.net/qq_41504815/article/details/113575728