package com.fgy.demo3; import java.util.function.Supplier; /** * 函数式接口:Supplier * get方法返回和泛型相同类型的值 */ public class Demo01Supplier { public static void main(String[] args) { String s = getString(() -> "胡歌"); System.out.println(s); int[] arr = {10, 11, 45, 12, 90}; int maxValue = getMax(() -> { int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (max < arr[i]) { max = arr[i]; } } return max; }); System.out.println(maxValue); } // 返回int类型的值 private static int getMax(Supplier<Integer> sup) { return sup.get(); } // 返回String类型的值 private static String getString(Supplier<String> sup) { return sup.get(); } }
package com.fgy.demo3; import java.util.function.Consumer; /** * 函数式接口:Consumer * accept方法使用和泛型相同类型的值 */ public class Demo02Consumer { public static void main(String[] args) { method("胡歌", name -> { // System.out.println(name); // 反转字符串 String reName = new StringBuilder(name).reverse().toString(); System.out.println(reName); }); } private static void method(String name, Consumer<String> con) { con.accept(name); } }
package com.fgy.demo3; import java.util.function.Consumer; /** * 函数式接口:Consumer的默认方法andThen * 作用:需要两个Consumer接口,可以把两个Consumer接口组合到一起,再对数据进行消费 */ public class Demo03ConsumerAndThen { public static void main(String[] args) { method("Hello Word", s -> System.out.println(s.toUpperCase()), s -> System.out.println(s.toLowerCase()) ); String[] arr = {"赵丽颖,女", "胡歌,男", "李泌,女"}; printInfo(arr, s -> System.out.print("姓名:" + s.split(",")[0]), s -> System.out.println(" 性别:" + s.split(",")[1]) ); } private static void method(String s, Consumer<String> con1, Consumer<String> con2) { // con1.accept(s); // con2.accept(s); con1.andThen(con2).accept(s); // HELLO WORD // hello word System.out.println("-----------"); con2.andThen(con1).accept(s); // hello word // HELLO WORD } /** * 格式化信息输出 */ private static void printInfo(String[] arr, Consumer<String> con1, Consumer<String> con2){ for (String s : arr) { con1.andThen(con2).accept(s); } } }
package com.fgy.demo3; import java.util.function.Predicate; /** * 函数式接口:Predicate * Test(T t)用来对指定类型的数据进行判断,符合条件返回true,不符合返回false */ public class Demo04Predicate { public static void main(String[] args) { boolean bool = checkString("abc", s -> s.length() >= 5); System.out.println(bool); } private static boolean checkString(String s, Predicate<String> pre) { return pre.test(s); } }
package com.fgy.demo3; import java.util.function.Predicate; /** * 函数式接口:Predicate的默认方法and * 表示并且关系,用来连接两个判断条件,相当于&&运算符 */ public class Demo05PredicateAnd { public static void main(String[] args) { boolean bool = checkString("abc", s -> s.length() >= 5, s -> s.contains("a") ); System.out.println(bool); } private static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2) { // return pre1.test(s) && pre2.test(s); return pre1.and(pre2).test(s); } }
package com.fgy.demo3; import java.util.function.Predicate; /** * 函数式接口:Predicate的默认方法or * 表示或者关系,用来连接两个判断条件,相当于||运算符 */ public class Demo06PredicateOr { public static void main(String[] args) { boolean bool = checkString("ab", s -> s.length() >= 5, s -> s.contains("a") ); System.out.println(bool); } private static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2) { // return pre1.test(s) || pre2.test(s); return pre1.or(pre2).test(s); } }
package com.fgy.demo3; import java.util.function.Predicate; /** * 函数式接口:Predicate的默认方法negate * 表示取反,相当!运算符 */ public class Demo07PredicateNegate { public static void main(String[] args) { boolean bool = checkString("abc", s -> s.length() >= 5); System.out.println(bool); } private static boolean checkString(String s, Predicate<String> pre) { // return !pre.test(s); return pre.negate().test(s); } }
package com.fgy.demo3; import java.util.function.Function; /** * 函数式接口:Function * R apply(T t)根据类型T的参数获取类型R的结果 */ public class Demo08Function { public static void main(String[] args) { change("1234", s -> Integer.parseInt(s)); } /** * Function<String, Integer> * 把String类型转换为Integer */ private static void change(String s, Function<String, Integer> fun) { Integer in = fun.apply(s); System.out.println(in); } }
package com.fgy.demo3; import java.util.function.Function; /** * 函数式接口:Function的默认方法andThen */ public class Demo08FunctionAndThen { public static void main(String[] args) { change("10", s -> Integer.parseInt(s) + 10, i -> "String:" + String.valueOf(i) ); } private static void change(String s, Function<String, Integer> fun1, Function<Integer, String> fun2) { // Integer in = fun1.apply(s) + 10; // 20 // String ss = "String" + fun2.apply(in); // String:20 String ss = fun1.andThen(fun2).apply(s); System.out.println(ss); } }