Consumer;
Function;
Predicate;
Supplier;
.
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class lambdaDome {
public static void main(String[] args) {
// Lambda supplier method 1
// supplier 生产型接口,接受什么值,返回什么值
Integer integer = method_01_Supplier(() -> 100);
System.out.println("Lambda 1 Return value is : " + integer);
// method Consumer method 1
// Consumer 消费型接口,消费传入的值
String str_num = "666";
method_01_Consumer(str_num, s -> System.out.println("Lambda 1 print value is :" + s));
// method Consumer method 2
String[] array_str = {"yh1,18", "yh2,19", "yh3,20"};
// print string : name and age
// 打印字符串,分别打印name字段和age字段
method_02_Consumer(array_str, s -> System.out.print("Name is : " + s.split(",")[0]),
s -> System.out.print("\tAge is : " + s.split(",")[1] + "\n"));
// method Predicted method 1 test method
// 判断大于10的数字,并返回结果
System.out.println("> 10 :" + method_Predicted_01(integer, i -> i > 10));
// method Predicted method 2 and method -> &&
// 判断大于10的,模2等于0的,并返回结果
System.out.println("> 10 and '%2 == 0' : " + method_Predicted_02(integer, i -> i > 10, j -> j % 2 == 0));
// method Predicted method 3 and method -> ||
// 判断大于10的,模3等于0的,并返回结果
System.out.println("> 10 and '%3 == 0' : " + method_Predicted_03(integer, i -> i > 10, j -> j % 3 == 0));
// method Predicted method 3 and method -> !
// 模3等于0的,对结果取反
System.out.println("> !result : " + method_Predicted_04(integer, k -> k % 3 == 0));
// 转换String to Integer
method_Function_apply(str_num,i-> Integer.parseInt(str_num));
// String to Integer to String
method_Function_andThen(str_num , (s)->Integer.parseInt(s),(s)->s+"");
String[] strings = {"迪丽热巴,23", "古力娜扎,27", "马尔扎哈,18", "赵丽颖,18", "杨幂,16",};
Integer sum = method_Function_andThen1(strings,(s)-> Integer.parseInt(s.split(",")[1]),(i)-> i + 100);
System.out.println(sum);
}
// @FunctionalInterface Supplier
// Supplier<T> : What 'T' is type ,return value is type
// T 是什么类型,supplier中方法get返回值就是什么类型
public static Integer method_01_Supplier(Supplier<Integer> supplier) {
return supplier.get();
}
public static Integer method_02_Supplier(Supplier<Integer> supplier) {
return supplier.get();
}
// @FunctionalInterface Consumer
// this is consumer interface
public static void method_01_Consumer(String str_number, Consumer<String> consumer) {
consumer.accept(str_number);
}
// this is consumer interface
public static void method_02_Consumer(String[] str_number, Consumer<String> consumer1, Consumer<String> consumer2) {
for (String message : str_number) {
consumer1.andThen(consumer2).accept(message);
}
}
// @FunctionalInterface Predicted
public static Boolean method_Predicted_01(Integer num, Predicate<Integer> predicate) {
return predicate.test(num);
}
public static Boolean method_Predicted_02(Integer num, Predicate<Integer> predicate1, Predicate<Integer> predicate2) {
return predicate1.and(predicate2).test(num);
}
public static Boolean method_Predicted_03(Integer num, Predicate<Integer> predicate1, Predicate<Integer> predicate2) {
return predicate1.or(predicate2).test(num);
}
public static Boolean method_Predicted_04(Integer num, Predicate<Integer> predicate1) {
return predicate1.negate().test(num);
}
// @FunctionalInterface Predicted
public static void method_Function_apply(String number , Function<String,Integer> fun){
System.out.println(number + "\tType : " +number.getClass().getName());
Integer integer = fun.apply(number);
System.out.println(integer + "\tType : " +integer.getClass().getName());
}
public static void method_Function_andThen(String number , Function<String,Integer> fun1,Function<Integer,String> fun2){
String apply = fun1.andThen(fun2).apply(number);
System.out.println(apply + " Type is : " +apply.getClass().getName());
}
public static Integer method_Function_andThen1(String[] str_arr , Function<String,Integer> fun1,Function<Integer,Integer> fun2){
Integer sum = 0 ;
for (String massage : str_arr)
sum += fun1.andThen(fun2).apply(massage);
return sum;
}
}