JAVA8新特性[第三季]-方法引用与构造器引用

版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/liudongdong0909。 https://blog.csdn.net/liudongdong0909/article/details/77422241

相关源码地址:https://github.com/liudongdong0909/java8/tree/master/java8-Lambda/src/com/donggua

一、方法引用

当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用。
注意: 实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致!
也可以把 方法引用 理解为Lambda表达式的另外一种表现形式

语法格式:使用操作符 ” :: ” 将方法名和对象或者类的名字分隔开来

  1. 对象 :: 实例方法名
  2. 类名 :: 静态方法名
  3. 类名 :: 实例方法名

例如:
1. (x) -> System.out.println(x); 等同于 System.out::println;
2. BinaryOperator bo = (x, y) -> Math.pow(x, y); 等同于 BinaryOperator bo = Math::pow;

1.1 对象 :: 实例方法名

第一个案例:

// 1.对象 :: 实例方法名
@Test
public void test1() {

    PrintStream out = System.out;
    Consumer<String> consumer = (x) -> out.println(x);
    consumer.accept("hello lamdba");

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

    // 1.对象 :: 实例方法名
    Consumer<String> consumer1 = out::println;
    consumer1.accept("hello method reference");

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

    Consumer<String> consumer2 = System.out::println;
    consumer2.accept("hello method reference println()");
}

执行结果:

hello lamdba
---------------------------
hello method reference
---------------------------
hello method reference println()

第二个案例:

// 1.对象 :: 实例方法名
@Test
public void test2() {
    Employee employee = new Employee(11, "林青霞", 28, 5555);
    Supplier<String> supplier = () -> employee.getName();
    System.out.println(supplier.get());

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

    Supplier<String> supplier1 = employee::getName;
    System.out.println(supplier1.get());

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

    Supplier<Integer> supplier2 = employee::getAge;
    System.out.println(supplier2.get());
}

执行结果:

林青霞
--------------------
林青霞
--------------------
28

1.2 类名 :: 静态方法名

// 2.类名 :: 静态方法名
@Test
public void test3() {
    BiFunction<Integer, Integer, Integer> biFunction = (x, y) -> Math.max(x, y);
    System.out.println(biFunction.apply(24, 9));

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

    BiFunction<Integer, Integer, Integer> biFunction1 = Math::max;
    System.out.println(biFunction1.apply(89, 22));

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

    Supplier<Double> random = Math::random;
    System.out.println(random.get());
}

执行结果:

24
---------------------
89
---------------------
0.8718820391381332

1.3 类名 :: 实例方法名

// 3.类名 :: 实例方法名
@Test
public  void test4(){
    BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y);
    System.out.println(biPredicate.test("dd", "DD"));

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

    BiPredicate<String, String> biPredicate1 = String::equals;
    System.out.println(biPredicate1.test("DD", "DD"));

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

    Consumer<Employee> consumer = (e) -> e.show(); // show方法是Employee中自定义的
    consumer.accept(new Employee());

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

    Consumer<Employee> consumer1 = Employee::show;
    consumer1.accept(new Employee());
}

执行结果:

false
--------------
true
--------------
测试方法引用!!!
--------------
测试方法引用!!!

二、构造器引用

与函数式接口相结合,自动与函数式接口中方法兼容。 可以把构造器引用赋值给定义的方法,与构造器参数 列表要与接口中抽象方法的参数列表一致!

语法格式:ClassName :: new
注意点: 构造器引用的参数列表, 必须与函数式接口中抽象方法的参数列表保持一致

2.1 第一个案例:

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

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

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

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

     Function<String, Employee> function = (name) -> new Employee(name);

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

     Function<String, Employee> function1 = Employee::new;
     Employee employee = function1.apply("冬瓜");
     System.out.println(employee.getName());

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

     BiFunction<Integer, Integer, Employee> biFunction = Employee::new;
     System.out.println(biFunction.apply(23, 34));
 }

执行结果:

Employee{id=0, name='null', age=0, salary=0.0, status=null}
---------------------
Employee{id=0, name='null', age=0, salary=0.0, status=null}
------------------------
============================
冬瓜
--------------------
Employee{id=23, name='null', age=34, salary=0.0, status=null}

2.2 第二个案例:

 @Test
 public void test3() {
     List<Employee> emps = Arrays.asList(
             new Employee(101, "林青霞", 28, 9889.99),
             new Employee(102, "东方不败", 29, 4329.85),
             new Employee(103, "周星驰", 40, 1233.88),
             new Employee(104, "大圣", 500, 5000.44),
             new Employee(105, "张无忌", 15, 3000.09)
     );

     emps.stream()
             .forEach((x) -> System.out.println(x));

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

     emps.stream()
             .forEach(System.out::println);

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

     emps.stream()
             .map((e) -> e.getAge())
             .sorted((x, y) -> Integer.compare(x, y))
             .forEach(System.out::println);

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

     emps.stream()
             .map(Employee::getAge)
             .sorted(Integer::compare)
             .forEach(System.out::println);

 }

执行结果:

Employee{id=101, name='林青霞', age=28, salary=9889.99, status=null}
Employee{id=102, name='东方不败', age=29, salary=4329.85, status=null}
Employee{id=103, name='周星驰', age=40, salary=1233.88, status=null}
Employee{id=104, name='大圣', age=500, salary=5000.44, status=null}
Employee{id=105, name='张无忌', age=15, salary=3000.09, status=null}
-----------------------
Employee{id=101, name='林青霞', age=28, salary=9889.99, status=null}
Employee{id=102, name='东方不败', age=29, salary=4329.85, status=null}
Employee{id=103, name='周星驰', age=40, salary=1233.88, status=null}
Employee{id=104, name='大圣', age=500, salary=5000.44, status=null}
Employee{id=105, name='张无忌', age=15, salary=3000.09, status=null}
-----------------------
15
28
29
40
500
------------------------
15
28
29
40
500

三、数组应用

语法格式: tyep[] :: new

 // 数组引用
 @Test
 public void test2() {
     Function<Integer, String[]> function = String[]::new;
     String[] apply = function.apply(90);
     System.out.println(apply.length);
 }

执行结果:

90

相关源码地址:https://github.com/liudongdong0909/java8/tree/master/java8-Lambda/src/com/donggua

猜你喜欢

转载自blog.csdn.net/liudongdong0909/article/details/77422241