JDK 8 新特性 | 方法引用、构造器引用和数组引用

方法引用

方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用(可以将方法引用理解为 Lambda 表达式的另外一种表现形式)。

  • 对象的引用 :: 实例方法名
(x) -> System.out.println(x);
// 等同于
System.out::println
  • 类名 :: 静态方法名
BinaryOperator<Double> bo = (x, y) -> Math.pow(x, y);
// 等同于
BinaryOperator<Double> bo = Math::pow;
  • 类名 :: 实例方法名
compare((x, y) -> x.equals(y), "abcdef", "abcdef");
// 等同于      
compare(String::equals, "abcdef", "abcdef");

注意:

  1. 方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
  2. Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式:ClassName::MethodName
简单举例

对象的引用 :: 实例方法名

/**                                                          
 * 对象的引用 :: 实例方法名                                            
 */                                                          
// 示例一                                                       
@Test                                                        
public void test1() {                                        
    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 test2() {                                        
    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;             
    con.accept("Hello World!");                              
}                                                            

运行结果:

张三
----------------------------------
张三


Hello World!
--------------------------------
Hello Java8!
Hello World!

类名 :: 静态方法名

/**                                                                          
 * 类名 :: 静态方法名                                                               
 */                                                                          
// 示例一                                                                       
@Test                                                                        
public void test3() {                                                        
    Comparator<Integer> com = (x, y) -> Integer.compare(x, y);               
    System.out.println(com.compare(1, 2));                                   

    System.out.println("-------------------------------------");             

    Comparator<Integer> com2 = Integer::compare;                             
    System.out.println(com2.compare(1, 2));                                  
}                                                                            

// 示例二                                                                       
@Test                                                                        
public void test4() {                                                        
    BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);       
    System.out.println(fun.apply(1.5, 22.2));                                

    System.out.println("--------------------------------------------------");

    BiFunction<Double, Double, Double> fun2 = Math::max;                     
    System.out.println(fun2.apply(1.2, 1.5));                                
}                       

运行结果:

-1
-------------------------------------
-1


22.2
--------------------------------------------------
1.5

类名 :: 实例方法名

/**                                                                 
 * 类名 :: 实例方法名                                                      
 */                                                                 
@Test                                                               
public void test5() {                                               
    BiPredicate<String, String> bp = (x, y) -> x.equals(y);         
    System.out.println(bp.test("abcde", "abcde"));                  

    System.out.println("-----------------------------------------");

    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("-----------------------------------------");

    Function<Employee, String> fun2 = Employee::show;               
    System.out.println(fun2.apply(new Employee()));                 

}                                                                   

运行结果:

true
-----------------------------------------
true
-----------------------------------------
测试方法引用!
-----------------------------------------
测试方法引用!
构造器引用

  • 格式: ClassName :: new
  • 构造器引用 :构造器的参数列表,需要与函数式接口中抽象方法的参数列表保持一致!
  • 与函数式接口相结合,自动与函数式接口中方法兼容。
  • 可以把构造器引用赋值给定义的方法。
简单示例
@Test                                                          
public void test1() {                                          
    Function<String, Employee> fun = Employee::new;            
    Employee emp = fun.apply("挖坑埋你");                          
    System.out.println(emp);                                   

    System.out.println("------------------------------------");

    BiFunction<String, Integer, Employee> fun2 = Employee::new;
    Employee emp2 = fun2.apply("挖坑埋你", 25);                    
    System.out.println(emp2);                                  
}                                                              

@Test                                                          
public void test2() {                                          
    Supplier<Employee> sup = () -> new Employee();             
    System.out.println(sup.get());                             

    System.out.println("------------------------------------");

    Supplier<Employee> sup2 = Employee::new;                   
    System.out.println(sup2.get());                            
}                                                              

输出结果:

Employee [id=0, name=挖坑埋你, age=0, salary=0.0]
------------------------------------
Employee [id=0, name=挖坑埋你, age=25, salary=0.0]


Employee [id=0, name=null, age=0, salary=0.0]
------------------------------------
Employee [id=0, name=null, age=0, salary=0.0]
数组引用

格式: type[] :: new

简单示例
@Test                                                            
public void test1() {                                            
    Function<Integer, String[]> fun = (args) -> new String[args];
    String[] strs = fun.apply(10);                               
    System.out.println(strs.length);                             

    System.out.println("--------------------------");            

    Function<Integer, Employee[]> fun2 = Employee[]::new;        
    Employee[] emps = fun2.apply(20);                            
    System.out.println(emps.length);                             
}                                                                

输出结果:

10
--------------------------
20
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值