JDK8新特性之方法引用与构造器引用快速入门

9 篇文章 0 订阅

一、方法引用

方法引用的写法可以理解为Lambda表达式的另外一种表现形式。

不熟悉Lambda表达式的可以参考JDK8新特性之Lambda表达式快速入门

方法引用的语法

使用操作符 “::” 将方法名和对象或类的名字分隔开来。语法有3种:

  • 对象::实例方法
  • 类::静态方法
  • 类::实例方法

使用时需满足的2个条件

条件一:若 Lambda 方法体中的功能,已经有方法提供了实现,可以使用方法引用。

条件二:方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致。


通过示例快速入门

对象::实例方法

通过示例来理解,示例1:

// 常规的写法
Consumer<String> con = (str) -> System.out.println(str);
con.accept("Hello World!");
// 方法引用的写法
Consumer<String> con2 = System.out::println;
con2.accept("Hello JDK8!");

解析上面这段示例,看怎么满足2个条件的。

System.out.println(str);
// 拆开写为
PrintStream ps = System.out;
ps.println(str);

println()是PrintStream已经实现好的实例方法。所以满足条件一。

看源码,对比Consumer的 accept() 和PrintStream的 println():

@FunctionalInterface
public interface Consumer<T> {
    
    void accept(T t);
    //……
}

public class PrintStream extends FilterOutputStream implements Appendable, Closeable {
    
    public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

可以看出它们都是一个入参、返回类型是void,所以满足条件二。

示例2:

@Test
public void test2(){
	Employee emp = new Employee(101, "张三", 18, 9999.99);

	Supplier<String> sup = () -> emp.getName();
	System.out.println(sup.get());
	
	Supplier<String> sup2 = emp::getName;
	System.out.println(sup2.get());
}

getName()方法是Employee类里已经实现好的方法,所以满足条件一。
getName()方法没有入参,会返回String类型,跟Supplier的方法一致,所以满足条件二。


类::静态方法

示例1:
我们常用的Comparator类的compare()方法,有2个入参,会返回一个int类型。

@FunctionalInterface
public interface Comparator<T> {
	
	int compare(T o1, T o2);
	//……
}

跟Integer类已提供好的静态方法compare()入参和返回值一致。

public final class Integer extends Number implements Comparable<Integer> {

	public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }
    //……
}

在满足使用方法引用的2个条件下,又满足第3个条件:是静态方法。那么Lambda表达式就可以写成类::静态方法。用法示例:

@Test
public void test1(){
	Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
	//静态方法引用
	Comparator<Integer> com2 = Integer::compare;
}

示例2:

@Test
public void test2(){
	BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
	System.out.println(fun.apply(1.5, 22.2));
		
	BiFunction<Double, Double, Double> fun2 = Math::max;
	System.out.println(fun2.apply(1.2, 1.5));
}

类::实例方法

在满足使用方法引用的2个条件下,又满足第3个条件:参数列表的第一个参数,是方法的调用者,第二个参数是方法的参数(或者没有第二个参数)。那么Lambda表达式就可以写成 类::实例方法

结合示例理解,示例1:

@Test
public void test1(){
	BiPredicate<String, String> bp = (x, y) -> x.equals(y);
	System.out.println(bp.test("abcde", "abcde"));
	
	BiPredicate<String, String> bp2 = String::equals;
	System.out.println(bp2.test("abc", "abc"));
}

Lambda表达式有2个入参x、y,第一个参数x是equals()方法的调用者,第二个参数y是equals()方法的参数。满足第3个条件。

示例2:

@Test
public void test2(){	
	Function<Employee, String> fun = (e) -> e.getName();
	System.out.println(fun.apply(new Employee()));
	
	Function<Employee, String> fun2 = Employee::getName;
	System.out.println(fun2.apply(new Employee()));
}

Lambda表达式有1个参数e,e是getName()方法的调用者,没有第2个参数。满足第3个条件。


二、构造器引用

类名::new

构造方法的参数列表,需要与函数式接口中参数列表保持一致。
通过示例理解:

public class Employee {

	private int id;
	private String name;
	private int age;
	private double salary;

	public Employee() {
	}

	public Employee(String name) {
		this.name = name;
	}

	public Employee(String name, int age) {
		this.name = name;
		this.age = age;
	}
	//...

使用示例:

@Test
public void test(){
	//无参构造器
//	Supplier<Employee> fun1 = () -> new Employee();
	Supplier<Employee> fun1 = Employee::new;
	Employee emp1 = fun1.get();
	System.out.println(emp1);

	//一个入参的构造器
//	Function<String, Employee> fun2 = (name) -> new Employee(name);
	Function<String, Employee> fun2 = Employee::new;
	Employee emp2 = fun2.apply("张三");
	System.out.println(emp2);

	//二个入参的构造器
//	BiFunction<String, Integer, Employee> fun3 = (name, age) -> new Employee(name, age);
	BiFunction<String, Integer, Employee> fun3 = Employee::new;
	Employee emp3 = fun3.apply("张三", 18);
	System.out.println(emp3);
}

三、数组引用

类型[]::new

通过示例理解:

@Test
public void test(){
//	Function<Integer, String[]> fun = (length) -> new String[length];
	Function<Integer, String[]> fun = String[]::new;
	String[] strArr = fun.apply(10);
	System.out.println(strArr.length);
	
//	Function<Integer, Employee[]> fun2 = (length) -> new Employee[length];
	Function<Integer, Employee[]> fun2 = Employee[]::new;
	Employee[] empArr = fun2.apply(10);
	System.out.println(empArr.length);
}

至此,本章结束。


  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值