public class FunctionDemo {
public static Integer testApply(Function<String,Integer> function , String str){
Integer i = function.apply(str);
return i;
}
public static Double testApply(Function<Integer,String> function1 ,Function<String,Double> function2, Integer i){
return function1.andThen(function2).apply(i);
}
public static void main(String[] args) {
testApply(t -> Integer.valueOf(t) , "123");
}
}
public class PredicateDemo {
public static boolean testPredicate(Predicate<Integer> function, String str) {
return function.test(Integer.valueOf(str));
}
public static void main(String[] args) {
testPredicate(t -> t > 1, "123");
}
}
public class SupplierDemo {
public static Integer testSupplierGet(Supplier<Integer> function){
return function.get();
}
public static void main(String[] args) {
String x = "123";
testSupplierGet(new Supplier<Integer>() {
@Override
public Integer get() {
return Integer.valueOf(x);
}
});
testSupplierGet(() ->Integer.valueOf(x));
}
}
public class ConsumerDemo {
public static void testAccept(Consumer<P> function, P p) {
function.accept(p);
}
public static void main(String[] args) {
P p = new P();
p.setAge(1);
testAccept(new Consumer<P>() {
@Override
public void accept(P p) {
p.setAge(123);
System.out.println(p.getAge());
}
}, p);
System.out.println(p.getAge());
Integer i = 10;
testAccept2(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println(integer);
integer = 100;//引用指向对象,对象变了
System.out.println(integer);
}
}, i);
System.out.println(i);//
String str = "10";
testAccept3(new Consumer<String>() {
@Override
public void accept(String str) {
System.out.println(str);
str = "100";
System.out.println(str);
}
}, str);
System.out.println(str);
}
public static void testAccept2(Consumer<Integer> function, Integer x) {
function.accept(x);
}
public static void testAccept3(Consumer<String> function, String x) {
function.accept(x);
}
@Data
@NoArgsConstructor
public static class P {
private int age;
}
}