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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| package java8;
import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier;
import org.junit.Test;
import javabean.Student;
public class FunctionTest { @Test public void exer22() { Supplier<Student> supplier2 = Student::new; System.out.println(supplier2.get()); } @Test public void test42() { Function<Integer, String> function2 = String::valueOf; System.out.println(function2.apply(1112)); } @Test public void test32() { Supplier<Double> supplier2 = Math::random; System.out.println(supplier2.get()); } @Test public void test12() { Consumer<String> consumer2 = System.out::println; consumer2.accept("lkjxlkcjccc"); } @Test public void exer4() { Predicate<Integer> predicate1 = new Predicate<Integer>() { @Override public boolean test(Integer t) { return t>= 60; } }; boolean test = predicate1.test(100); System.out.println(test); Predicate<Integer> predicate2 = t -> t >= 60; System.out.println(predicate2.test(100)); } @Test public void test5() { Predicate<Integer> predicate1 = new Predicate<Integer>() { @Override public boolean test(Integer t) { return t % 2 == 0; } }; boolean test = predicate1.test(83); System.out.println(test); Predicate<Integer> predicate2 = t -> t % 2 == 0; System.out.println(predicate2.test(20)); } @Test public void exer3() { Function<Student, String> function1 = new Function<Student, String>() { @Override public String apply(Student t) { return t.getName() + ":" + t.getScore(); } }; String apply = function1.apply(new Student(2, "小刚", 5, 80)); System.out.println(apply); Function<Student, String> function2 = t -> t.getName() + ":" + t.getScore(); System.out.println(function2.apply(new Student(1, "小花", 2, 100))); } @Test public void test4() { Function<Integer, String> function1 = new Function<Integer, String>() { @Override public String apply(Integer t) { return String.valueOf(t); } }; String apply = function1.apply(9238); System.out.println(apply); Function<Integer, String> function2 = t -> String.valueOf(t); System.out.println(function2.apply(1112)); } @Test public void exer2() { Supplier<Student> supplier1 = new Supplier<Student>() { @Override public Student get() { return new Student(); } }; Student student = supplier1.get(); System.out.println(student); Supplier<Student> supplier2 = () -> new Student(); System.out.println(supplier2.get()); } @Test public void test3() { Supplier<Double> supplier = new Supplier<Double>() { @Override public Double get() { return Math.random(); } }; System.out.println(supplier.get()); Supplier<Double> supplier2 = () -> Math.random(); System.out.println(supplier2.get()); } @Test public void test2() { Supplier<Integer> supplier1 = new Supplier<Integer>() { @Override public Integer get() { return 100; } }; Integer integer = supplier1.get(); System.out.println(integer); Supplier<Integer> supplier2 = () -> 100; Integer integer2 = supplier2.get(); System.out.println(integer2); } @Test public void exer1() { Consumer<Student> consumer1 = new Consumer<Student>() { @Override public void accept(Student t) { System.out.println(t); } }; consumer1.accept(new Student()); Consumer<Student> consumer2 = t -> System.out.println(t); consumer2.accept(new Student(1, "小花", 2, 50)); } @Test public void test1() { Consumer<String> consumer1 = new Consumer<String>() { @Override public void accept(String t) { System.out.println(t); } }; consumer1.accept("alsdkjfalksdjf"); Consumer<String> consumer2 = t -> System.out.println(t); consumer2.accept("lkjxlkcjccc"); } }
|