[关闭]
@huangyichun 2017-05-03T21:36:13.000000Z 字数 2878 阅读 954

Java8 方法引用与构造器引用

Java8


  1. package java_8;
  2. import org.junit.Test;
  3. import java.io.PrintStream;
  4. import java.util.Comparator;
  5. import java.util.function.*;
  6. import java.util.function.Function;
  7. /**
  8. * 方法引用:若Lambda体中的内容有方法已经实现了,那么我们可以使用"方法引用"
  9. * (可以理解为方法引用时Lambda表达式的另外一种表现形式
  10. *
  11. * 主要有三种语法格式:
  12. *
  13. * 对象::实例方法名
  14. *
  15. * 类::静态方法名
  16. *
  17. * 类::实例方法名
  18. *
  19. * 注意:
  20. * 1. Lambda体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的函数列表和返回值类型保存一致
  21. * 2.若Lambda参数列表中的第一个参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用ClassName::method
  22. *
  23. * 二、构造器引用
  24. * 格式:
  25. * ClassName::new
  26. * 注意:需要调用的构造器方法与函数式接口中抽象方法的参数列表保持一致
  27. *
  28. * 三、数组引用
  29. * Type::new;
  30. */
  31. public class TestMethodRef {
  32. //数组引用:
  33. @Test
  34. public void test7(){
  35. Function<Integer, String[]> fun = x -> new String[x];
  36. String[] strs = fun.apply(10);
  37. System.out.println(strs.length);
  38. Function<Integer,String[]> fun1 = String[]::new;
  39. strs = fun1.apply(20);
  40. System.out.println(strs.length);
  41. }
  42. //构造器引用
  43. @Test
  44. public void test5(){
  45. Supplier<Employee> sup = ()-> new Employee();
  46. Employee emp = sup.get();
  47. //构造器引用
  48. //根据参数列表自动匹配构造器
  49. Supplier<Employee> sup2 = Employee::new;
  50. emp = sup2.get();
  51. System.out.println(emp);
  52. }
  53. @Test
  54. public void test6(){
  55. Function<Integer,Employee> func = x -> new Employee(x);
  56. Employee emp = func.apply(10);
  57. System.out.println(emp);
  58. Function<Integer,Employee> func1 = Employee :: new;
  59. emp = func1.apply(10);
  60. System.out.println(emp);
  61. // BiFunction<Integer, Integer, Employee> bf = Employee::new;编译错误,没有两个Integer构造器
  62. }
  63. //对象::实例方法名
  64. @Test
  65. public void test1(){
  66. Consumer<String> con = x -> System.out.println(x);
  67. PrintStream ps = System.out; //打印流
  68. //前提条件: Consumer中的方法体参数与返回值要与ps.println方法中的参数和返回值类型相同
  69. //Consumer: void accept(T t);在这里T为String
  70. //PrintStream: public void println(String x)
  71. //两者传入的参数都为String,返回值都为void所以满足,可以使用方法引用
  72. Consumer<String> con1 = ps::println;
  73. Consumer<String> con2 = System.out::println;//这三种方式结果相同
  74. con.accept("huang");
  75. con1.accept("huang");
  76. con2.accept("huang");
  77. }
  78. @Test
  79. public void test2(){
  80. Employee emp = new Employee();
  81. Supplier<String> sup = () -> emp.getName();
  82. Supplier<String> sup2 = emp::getName;
  83. }
  84. //---------------------------------------
  85. //类::静态方法名
  86. @Test
  87. public void test3(){
  88. Comparator<Integer> com = (x, y) ->Integer.compare(x, y);
  89. //前提条件:和上面相同
  90. Comparator<Integer> com1 = Integer::compare;
  91. }
  92. //类::实例方法名
  93. @Test
  94. public void test4(){
  95. BiPredicate<String, String> bp = (x, y) -> x.equals(y);
  96. boolean bool = bp.test(new String("huang"),"huang");
  97. System.out.println(bool);
  98. //前提:第一个参数是实例方法的调用者,第二个参数是实例方法的参数
  99. //例如 x 是equal方法的调用者,y是实例方法的参数
  100. BiPredicate<String,String> bp2 = String::equals;
  101. bool = bp2.test("huang","huang");
  102. System.out.println(bool);
  103. }
  104. }
  1. package java_8;
  2. public class Employee {
  3. private int id;
  4. private String name;
  5. private int age;
  6. private double salary;
  7. public Employee() {
  8. }
  9. public Employee(int id){
  10. this.id = id;
  11. }
  12. @Override
  13. public String toString() {
  14. return "Employee{" +
  15. "id=" + id +
  16. ", name='" + name + '\'' +
  17. ", age=" + age +
  18. ", salary=" + salary +
  19. '}';
  20. }
  21. public Employee(String name, int age, double salary) {
  22. this.name = name;
  23. this.age = age;
  24. this.salary = salary;
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public int getAge() {
  33. return age;
  34. }
  35. public void setAge(int age) {
  36. this.age = age;
  37. }
  38. public double getSalary() {
  39. return salary;
  40. }
  41. public void setSalary(double salary) {
  42. this.salary = salary;
  43. }
  44. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注