当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
(实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致!)
方法引用:使用操作符 “ ::” 将方法名和对象或类的名字分隔开来。
如下三种主要使用情况 :
 对象 :: 实例方法
 类 :: 静态方法
 类 :: 实例方法

案例

方法

  1. import e_Lambda表达式.entity.Employee;
  2. import org.junit.Test;
  3. import java.io.PrintStream;
  4. import java.util.Comparator;
  5. import java.util.function.*;
  6. /*
  7. 案例:ZJJ_JavaBasic_2019/10/30_11:23:46_thcfi
  8. * 一、方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用
  9. * (可以将方法引用理解为 Lambda 表达式的另外一种表现形式)
  10. *
  11. * 1. 对象的引用 :: 实例方法名
  12. *
  13. * 2. 类名 :: 静态方法名
  14. *
  15. * 3. 类名 :: 实例方法名
  16. *
  17. * 注意:
  18. * ①方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
  19. * ②若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
  20. *
  21. * 二、构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
  22. *
  23. * 1. 类名 :: new
  24. *
  25. * 三、数组引用
  26. *
  27. * 类型[] :: new;
  28. *
  29. *
  30. */
  31. public class TestMethodRef {
  32. //数组引用
  33. @Test
  34. public void test8() {
  35. Function<Integer, String[]> fun = (args) -> new String[args];
  36. String[] strs = fun.apply(10);
  37. System.out.println(strs.length);
  38. System.out.println("--------------------------");
  39. Function<Integer, Employee[]> fun2 = Employee[]::new;
  40. Employee[] emps = fun2.apply(20);
  41. System.out.println(emps.length);
  42. }
  43. //构造器引用
  44. @Test
  45. public void test7() {
  46. Function<String, Employee> fun = Employee::new;
  47. BiFunction<String, Integer, Employee> fun2 = Employee::new;
  48. }
  49. @Test
  50. public void test6() {
  51. Supplier<Employee> sup = () -> new Employee();
  52. System.out.println(sup.get());
  53. System.out.println("------------------------------------");
  54. Supplier<Employee> sup2 = Employee::new;
  55. System.out.println(sup2.get());
  56. }
  57. //类名 :: 实例方法名
  58. @Test
  59. public void test5() {
  60. BiPredicate<String, String> bp = (x, y) -> x.equals(y);
  61. System.out.println(bp.test("abcde", "abcde"));
  62. System.out.println("-----------------------------------------");
  63. BiPredicate<String, String> bp2 = String::equals;
  64. System.out.println(bp2.test("abc", "abc"));
  65. System.out.println("-----------------------------------------");
  66. Function<Employee, String> fun = (e) -> e.show();
  67. System.out.println(fun.apply(new Employee()));
  68. System.out.println("-----------------------------------------");
  69. Function<Employee, String> fun2 = Employee::show;
  70. System.out.println(fun2.apply(new Employee()));
  71. }
  72. //类名 :: 静态方法名
  73. @Test
  74. public void test4() {
  75. Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
  76. int compare = com.compare(1, 2);
  77. System.out.println("compare = " + compare);
  78. System.out.println("-------------------------------------");
  79. Comparator<Integer> com2 = Integer::compare;
  80. int compare1 = com2.compare(1, 2);
  81. System.out.println("compare1 = " + compare1);
  82. }
  83. @Test
  84. public void test3() {
  85. BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
  86. System.out.println(fun.apply(1.5, 22.2));
  87. System.out.println("--------------------------------------------------");
  88. BiFunction<Double, Double, Double> fun2 = Math::max;
  89. System.out.println(fun2.apply(1.2, 1.5));
  90. }
  91. //对象的引用 :: 实例方法名
  92. @Test
  93. public void test2() {
  94. Employee emp = new Employee(101, "张三", 18, 9999.99);
  95. Supplier<String> sup = () -> emp.getName();
  96. System.out.println(sup.get());
  97. System.out.println("----------------------------------");
  98. Supplier<String> sup2 = emp::getName;
  99. System.out.println(sup2.get());
  100. }
  101. @Test
  102. public void test1() {
  103. PrintStream ps = System.out;
  104. //消费性接口
  105. Consumer<String> con = (str) -> ps.println(str);
  106. con.accept("Hello World!");
  107. System.out.println("--------------------------------");
  108. Consumer<String> con2 = ps::println;
  109. con2.accept("Hello Java8!");
  110. Consumer<String> con3 = System.out::println;
  111. con3.accept("11111111");
  112. }
  113. }

实体类


public class Employee {

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

    //****************
    public Employee() {
    }

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

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

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Employee(int id, String name, int age, double salary) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public Employee(int id, String name, int age, double salary, Status status) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.status = status;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String show() {
        return "测试方法引用!";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        long temp;
        temp = Double.doubleToLongBits(salary);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (age != other.age)
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }

    public enum Status {
        FREE, BUSY, VOCATION;
    }
}