1、Supplier接口
java.util.function.Supplier<T> 接口仅包含一个无参的方法:T get() 。用来获取一个泛型参数指定类型的对象数据。
///////////////////////Supplier接口////////////////////////// private static String getStringSupplier(Supplier<String> function) {return function.get();}private static void testSupplier(){String msgA = "Hello";String msgB = "World";System.out.println(getStringSupplier(() -> msgA + msgB));}// 求一个数组的最大值 private static int getMax(Supplier<Integer> function){return function.get();}private static void runGetMax(){int[] arr = {2,3,4,52,333,23};int maxNum = getMax(() -> {int max = arr[0];for (int i : arr){if (i > max){max = i;}}return max;});System.out.println(maxNum);}
2、Consumer接口
java.util.function.Consumer<T> 接口则正好与Supplier接口相反,它不是生产一个数据,而是消费一个数据,其数据类型由泛型决定,其抽象方法为void accept(T t)。
如果一个方法的参数和返回值全都是 Consumer 类型,那么就可以实现效果:消费数据的时候,首先做一个操作,然后再做一个操作,实现组合。而这个方法就是 Consumer 接口中的default方法 andThen() 。
///////////////////////Consumer接口////////////////////////// private static void consumerString(Consumer<String> function){function.accept("Hello");}private static void testConsumer(){consumerString(s -> System.out.println(s));}// Consumer.andThen() 方法,接收和返回的对象都是Consumer private static void andThenAccept(Consumer<String> one, Consumer<String> two){one.andThen(two).accept("Hello");}// 先转成小写,再装换成大写 private static void testAndThenAccept(){andThenAccept(s -> System.out.println(s.toLowerCase()),s -> System.out.println(s.toUpperCase()));}// 下面的字符串数组当中存有多条信息,请按照格式“ 姓名:XX。性别:XX。”的格式将信息打印出来。 // 要求将打印姓名的动作作为第一个 Consumer 接口的Lambda实例, // 将打印性别的动作作为第二个 Consumer 接口的Lambda实例,将两个 Consumer 接口按照顺序“拼接”到一起 // .forEach(System.out::println); private static void formatPrint1(Consumer<String> one, Consumer<String> two, String s){one.andThen(two).accept(s);}private static void runFormatPrint1(){String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" };for (String i : array) {formatPrint1(s -> System.out.print("姓名:" + s.split(",")[0] + "。"),s -> System.out.print("性别:" + s.split(",")[1] + "\n"),i);}}private static void formatPrint2(Consumer<String> one, Consumer<String> two, String[] array){for(String s : array){one.andThen(two).accept(s);}}private static void runFormatPrint2(){String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" };formatPrint2(s -> System.out.print("姓名:" + s.split(",")[0] + "。"),s -> System.out.print("性别:" + s.split(",")[1] + "\n"),array);}
3、Predicate接口
有时候我们需要对某种类型的数据进行判断,从而得到一个boolean值结果。这时可以使用
java.util.function.Predicate<T> 接口。他有一个抽象方法boolean test(T t) 用来条件判断。
还有三个默认方法:与、或、非
default Predicate<T> and(Predicate<? super T> other) {Objects.requireNonNull(other);return (t) ‐> test(t) && other.test(t);}default Predicate<T> or(Predicate<? super T> other) {Objects.requireNonNull(other);return (t) -> test(t) || other.test(t);}default Predicate<T> negate() {return (t) -> !test(t);}
例子:
///////////////////////Predicate接口////////////////////////// private static void predicateTest(Predicate<String> function){boolean veryLong = function.test("Hello");System.out.println("这个字符串很长吗? " + veryLong);}private static void runPredicateTest(){predicateTest(s -> s.length() > 5);}// 判断一个字符串既要包含大写“H”,又要包含大写“W” private static void predicateAndTest(Predicate<String> one, Predicate<String> two, String string){System.out.println("既包含大写“H”,又包含大写“W”吗? " + one.and(two).test(string));}private static void runPredicateAndTest(){String string = "Hello Java";predicateAndTest(s -> s.contains("H"), s -> s.contains("W"), string);}// 判断一个字符串包含大写“H” 或者 包含大写“W” private static void predicateOrTest(Predicate<String> one, Predicate<String> two, String string){System.out.println("包含大写“H”或者包含大写“W”吗? " + one.or(two).test(string));}private static void runPredicateOrTest(){String string = "Hello Java";predicateOrTest(s -> s.contains("H"), s -> s.contains("W"), string);}//字符串很长吗 private static void predicateNegateTest(Predicate<String> function){boolean veryLong = function.negate().test("Hello");System.out.println("这个字符串很长吗? " + veryLong);}private static void runPredicateNegateTest(){predicateNegateTest(s -> s.length() <= 5);}// 数组当中有多条“姓名+性别”的信息如下, // 请通过 Predicate 接口的拼装将符合要求的字符串筛选到集合ArrayList 中, // 需要同时满足两个条件:1. 必须为女生;2. 姓名为4个字。 private static List filterByPredicate(Predicate<String> one, Predicate<String> two, String[] array){List<String> list = new ArrayList<String>();for (String string : array){if (one.and(two).test(string)){list.add(string);}}return list;}private static void runFilterByPredicate(){String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女" };List<String> list = filterByPredicate(s -> "女".equals(s.split(",")[1]),s -> s.split(",")[0].length() == 4,array);System.out.println(list.toString());}
4、Function接口
java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。
Function 接口中最主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。使用的场景例如:将 String 类型转换为 Integer 类型。
Function 接口中有一个默认的 andThen 方法,用来进行组合操作。
///////////////////////Function接口////////////////////////// private static void functionApply(Function<String, Integer> function){int num = function.apply("30");System.out.println(num + 20);}private static void runFunctionApply(){functionApply(s -> Integer.parseInt(s));}// 先将一个字符串转为int,然后再乘10 private static void functionAndThenApply(Function<String, Integer> one, Function<Integer, Integer> two){// 第一个操作是将字符串解析成为int数字,第二个操作是乘以10。两个操作通过 andThen 按照前后顺序组合到了一起。 System.out.println(one.andThen(two).apply("10"));}private static void runFunctionAndThenApply(){functionAndThenApply(s -> Integer.parseInt(s), integer -> integer * 10);}// 1. 将字符串截取数字年龄部分,得到字符串; // 2. 将上一步的字符串转换成为int类型的数字; // 3. 将上一步的int数字累加100,得到结果int数字。 private static void convertByFunction(Function<String, String> one,Function<String, Integer> two,Function<Integer, Integer> three){System.out.println(one.andThen(two).andThen(three).apply("赵丽颖,20"));}private static void runConvertByFunction(){convertByFunction(s -> s.split(",")[1], s -> Integer.parseInt(s), integer -> integer += 100);}
延迟方法与终结方法
函数式接口当中,方法可以分成两种:
延迟方法:
只是在拼接Lambda函数模型的方法,并不立即执行得到结果。
终结方法:
根据拼好的Lambda函数模型,立即执行得到结果值的方法。
总结:
统一运行一遍:
public static void main(String[] args) {System.out.println("Supplier 接口==================");testSupplier();runGetMax();System.out.println();System.out.println("Consumer 接口==================");testConsumer();testAndThenAccept();runFormatPrint1();runFormatPrint2();System.out.println();System.out.println("Predicate 接口==================");runPredicateTest();runPredicateAndTest();runPredicateOrTest();runPredicateNegateTest();runFilterByPredicate();System.out.println();System.out.println("Function 接口==================");runFunctionApply();runFunctionAndThenApply();runConvertByFunction();}
输出:
Supplier 接口==================HelloWorld333Consumer 接口==================HellohelloHELLO姓名:迪丽热巴。性别:女姓名:古力娜扎。性别:女姓名:马尔扎哈。性别:男姓名:迪丽热巴。性别:女姓名:古力娜扎。性别:女姓名:马尔扎哈。性别:男Predicate 接口==================这个字符串很长吗? false既包含大写“H”,又包含大写“W”吗? false包含大写“H”或者包含大写“W”吗? true这个字符串很长吗? false[迪丽热巴,女, 古力娜扎,女]Function 接口==================50100120
暂无评论数据