方法引用 构造器引用 数组引用

方法引用与构造器引用

若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用(可以将方法引用理解为 Lambda 表达式的另外一种表现形式)
方法引用: 使用操作符 “::” 将方法名和对象或类的名字分隔开来。

# 例如  
(x) -> System.out.println(x); 等同于  
System.out::println.

方法引用

注意:

  1. 方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
  2. 若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
  • 对象::实例方法
# 对象的引用 :: 实例方法名
@Test
public void test2(){
	Employee emp = new Employee(101, "张三", 18, 9999.99);
	
	Supplier<String> sup = () -> emp.getName();
	System.out.println(sup.get());
	
	System.out.println("----------------------------------");
	// 直接使用对象::实例方法来引用
	Supplier<String> sup2 = emp::getName;
	System.out.println(sup2.get());
}

@Test
public void test1(){
// 标准打印流对象 
	PrintStream ps = System.out;
	Consumer<String> con = (str) -> ps.println(str);
	con.accept("Hello World!");
	
	System.out.println("--------------------------------");
	// 对象::实例名引用
	Consumer<String> con2 = ps::println;
	con2.accept("Hello Java8!");
	
	Consumer<String> con3 = System.out::println;
}
  • 类::静态方法
# 类名 :: 静态方法名
@Test
public void test4(){
	Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
	
	System.out.println("-------------------------------------");
	
	Comparator<Integer> com2 = Integer::compare;
}

@Test
public void test3(){
//  (x, y) -> Math.max(x, y) 调用funcation函数的子接口 比较x,y的大小  
	BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
	System.out.println(fun.apply(1.5, 22.2));
	
	System.out.println("--------------------------------------------------");
	// Math类直接调用静态方法 Math::max 获取最大值 
	BiFunction<Double, Double, Double> fun2 = Math::max;
	System.out.println(fun2.apply(1.2, 1.5));
}
  • 类::实例方法
# 类名 :: 实例方法名
@Test
public void test5(){
//  BiPredicate断言式接口的子接口 
	BiPredicate<String, String> bp = (x, y) -> x.equals(y);
	System.out.println(bp.test("abcde", "abcde"));
	
	System.out.println("-----------------------------------------");
	// 直接使用类名::实例方法名,来调用equals()方法
	BiPredicate<String, String> bp2 = String::equals;
	System.out.println(bp2.test("abc", "abc"));
	
	System.out.println("-----------------------------------------");
	
	
	Function<Employee, String> fun = (e) -> e.show();
	System.out.println(fun.apply(new Employee()));
	
	System.out.println("-----------------------------------------");
	// 创建函数调用show方法   直接调用Employee::show类中的show方法  
	Function<Employee, String> fun2 = Employee::show;
	System.out.println(fun2.apply(new Employee()));
	
}

构造器引用

构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
类名::new 方式调用构造器

# 构造器引用  
@Test
public void test7(){
    // 调用一个参数的构造器
	Function<String, Employee> fun = Employee::new;
	// 两个参数的构造器,参数类型为String, Integer,构建对象为Employee
	BiFunction<String, Integer, Employee> fun2 = Employee::new;
}

@Test
public void test6(){
// Supplier返回对象  () -> new Employee() 新建一个对象 
	Supplier<Employee> sup = () -> new Employee();
	System.out.println(sup.get());
	
	System.out.println("------------------------------------");
	// 类名::new 方式调用构造器  空参构造器  
	Supplier<Employee> sup2 = Employee::new;
	System.out.println(sup2.get());
}

数组引用

类型[] :: new

# 数组引用
@Test
public void test8(){
    // 构建创建String[] 数组的函数  
	Function<Integer, String[]> fun = (args) -> new String[args];
	// 设置数组的大小 
	String[] strs = fun.apply(10);
	System.out.println(strs.length);
	
	System.out.println("--------------------------");
	// 构建创建String[] 数组的函数  
	Function<Integer, Employee[]> fun2 = Employee[] :: new;
	// 数组长度设置为20  
	Employee[] emps = fun2.apply(20);
	System.out.println(emps.length);
}
<think>我们需要解释构造器方法引用的概念,并给出示例。 构造器方法引用Java 8方法引用的一种,用于简化Lambda表达式创建对象的过程。 语法:ClassName::new 根据参数类型匹配构造器。 示例:使用Supplier接口的无参构造,或Function接口的有参构造等。 规则:必须按照要求,在最后提出3-5个相关问题,以“§§相关问题§§”开头,然后列出问题。</think>### 构造器方法引用Java构造器方法引用Java 8 引入的一种特殊方法引用语法,用于简化通过 Lambda 表达式创建对象的操作。它允许直接引用类的构造器,替代显式的 Lambda 写法,使代码更简洁。 #### 核心语法 ```java ClassName::new ``` #### 三种常见使用场景 1. **无参构造器引用**(匹配 `Supplier` 函数式接口) ```java // Lambda 写法 Supplier<List<String>> supplier = () -> new ArrayList<>(); // 构造器引用写法 Supplier<List<String>> supplier = ArrayList::new; ``` 2. **单参构造器引用**(匹配 `Function` 接口) ```java // Lambda 写法 Function<String, Integer> func = s -> new Integer(s); // 构造器引用写法 Function<String, Integer> func = Integer::new; ``` 3. **多参构造器引用**(匹配自定义函数式接口) ```java // 自定义函数接口 @FunctionalInterface interface TriFunction<A,B,C,R> { R apply(A a, B b, C c); } // 引用三参数构造器 TriFunction<String, Integer, Double, Product> creator = Product::new; ``` #### 底层原理 构造器引用本质是编译器自动生成实现函数式接口的 Lambda 表达式: ```java // 编译器转换示例 Function<String, Integer> func = Integer::new; // 等价于 Function<String, Integer> func = (s) -> new Integer(s); ``` #### 注意事项 1. 参数匹配:构造器参数必须与函数式接口抽象方法的参数完全匹配 2. 类型推断:编译器根据上下文自动推断泛型类型 3. 异常处理:构造器若抛出检查异常,需在函数式接口中使用 `@FunctionalInterface` 声明异常类型 4. 数组构造:支持数组构造器引用(如 `int[]::new`) #### 示例代码 ```java import java.util.function.*; class Person { private String name; public Person() { this.name = "Unknown"; } public Person(String name) { this.name = name; } public static void main(String[] args) { // 无参构造器引用 Supplier<Person> personSupplier = Person::new; Person p1 = personSupplier.get(); // 调用默认构造器 // 单参构造器引用 Function<String, Person> nameToPerson = Person::new; Person p2 = nameToPerson.apply("Alice"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值