1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
   | public class MethodReferences {
                          @Test     public void test1(){                  Consumer<String> stringConsumer = new Consumer<String>(){             @Override             public void accept(String s) {                 System.out.println(s);             }         };
                   Consumer<String> con1 = s -> System.out.println(s);         con1.accept("南京");
                   PrintStream out = System.out;         Consumer<String> con2 = out :: println;                  con2.accept("北京");     }
                     @Test     public void test2(){
                   Employee tom = new Employee("Tom");         Supplier<String> s = new Supplier<String>() {             @Override             public String get() {                 return tom.getName();             }         };         System.out.println(s.get());
                   Supplier<String> s1 = () -> tom.getName();         System.out.println(s1.get());
                   Supplier<String> s2 = tom :: getName;         System.out.println(s2.get());     }
                     @Test     public void test3(){
                   Comparator<Integer> com = new Comparator<Integer>() {             @Override             public int compare(Integer o1, Integer o2) {                 return Integer.compare(o1,o2);             }         };         System.out.println(com.compare(12,3));
                   Comparator<Integer> com1 = (o1,o2) -> Integer.compare(o1,o2);         System.out.println(com1.compare(3,5));
                   Comparator<Integer> com2 = Integer::compare;         System.out.println(com2.compare(5,9));     }
                @Test     public void test4(){
                   Function<Double,Long> fun = new Function<Double, Long>() {             @Override             public Long apply(Double d) {                 return Math.round(d);             }         };         System.out.println(fun.apply(15.63));
                   Function<Double,Long> fun1 = d -> Math.round(d);         System.out.println(fun1.apply(55.3));
                   Function<Double,Long> fun2 = Math::round;         System.out.println(fun2.apply(42.6));
      }
                     @Test     public void test5(){                  Comparator<String> com = (s1,s2) -> s1.compareTo(s2);         System.out.println(com.compare("abc", "abd"));
                   Comparator<String> com1 = String :: compareTo;         System.out.println(com1.compare("adm", "abg"));     }
                @Test     public void test6(){                  BiPredicate<String,String> pre = new BiPredicate<String, String>() {             @Override             public boolean test(String s1, String s2) {                 return s1.equals(s2);             }         };         System.out.println(pre.test("abc", "abc"));
                   BiPredicate<String,String> pre1 = (s1, s2) -> s1.equals(s2);         System.out.println(pre1.test("dbd", "abd"));
                   BiPredicate<String,String> pre2 = String :: equals;         System.out.println(pre2.test("abc", "abc"));     }
                @Test     public void test7(){         Employee employee = new Employee("jack");
                   Function<Employee,String> fun = new Function<Employee, String>() {             @Override             public String apply(Employee e) {                 return e.getName();             }         };
                   Function<Employee,String> fun1 = e -> e.getName();         System.out.println(fun1.apply(employee));
                   Function<Employee,String> fun2 = Employee :: getName;         System.out.println(fun2.apply(employee));     } }
  |