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 160 161 162 163
| public class MethodReferences {
@Test public void t1() { Consumer<String> consumer = new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } }; consumer.accept("上海市");
Consumer<String> consumerL = str -> System.out.println(str); consumerL.accept("上海");
PrintStream out = System.out; Consumer<String> consumerM = out::println; consumerM.accept("shanghai"); }
@Test public void t2() { Person person = new Person("小威", LocalDate.of(2016, 9, 1));
Supplier<String> supplier = new Supplier<String>() { @Override public String get() { return person.getName(); } }; System.out.println(supplier.get());
Supplier<String> supplierL = () -> person.getName(); System.out.println(supplierL.get());
Supplier<String> supplierM = person::getName; System.out.println(supplierM.get()); }
@Test public void t3() { Comparator<Integer> comparator = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return Integer.compare(o1, o2); } }; System.out.println(comparator.compare(520, 1314));
Comparator<Integer> comparatorL = (o1, o2) -> Integer.compare(o1, o2); System.out.println(comparatorL.compare(100, 99));
Comparator<Integer> comparatorM = Integer::compareTo; System.out.println(comparatorM.compare(123, 321)); }
@Test public void t4() { Function<Double, Long> function = new Function<Double, Long>() { @Override public Long apply(Double aDouble) { return Math.round(aDouble); } }; System.out.println(function.apply(13.7));
Function<Double, Long> functionL = d -> Math.round(d); System.out.println(functionL.apply(13.4));
Function<Double, Long> functionM = Math::round; System.out.println(functionM.apply(13.9)); }
@Test public void t5() { Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }; System.out.println(comparator.compare("abc", "abd"));
Comparator<String> comparatorL = (o1, o2) -> o1.compareTo(o2); System.out.println(comparatorL.compare("abc", "abd"));
Comparator<String> comparatorM = String::compareTo; System.out.println(comparatorM.compare("abd", "abc")); }
@Test public void t6() { BiPredicate<String, String> biPredicate = new BiPredicate<String, String>() { @Override public boolean test(String s, String s2) { return s.equals(s2); } }; System.out.println(biPredicate.test("abc", "abd"));
BiPredicate<String, String> biPredicateL = (s1, s2) -> s1.equals(s2); System.out.println(biPredicateL.test("abc", "abd"));
BiPredicate<String, String> biPredicateM = String::equals; System.out.println(biPredicateM.test("abc", "abd")); }
@Test public void t7() { Person person = new Person("Vincent", LocalDate.of(1991, 1, 28));
Function<Person, String> function = new Function<Person, String>() { @Override public String apply(Person person) { return person.getName(); } }; System.out.println(function.apply(person));
Function<Person, String> functionL = p -> p.getName(); System.out.println(functionL.apply(person));
Function<Person, String> functionM = Person::getName; System.out.println(functionM.apply(person)); } }
|