JAVA8 Function、Consumer、Predicate、Supplier接口

来源:互联网 发布:graph cut算法原理 编辑:程序博客网 时间:2024/06/07 13:24

这里写图片描述

如果你在探索 在JAVA8中 支持lambda表达式的接口,你会在java.util.function包下发现像
Function、Supplier、Consumer、Predicate 等接口。这些接口有一个抽象方法,会被定义的lambda表达式重写。

一、Function接口

Function接口接受一个输入参数,返回一个结果。

package java.util.function;import java.util.Objects;@FunctionalInterfacepublic interface Function<T, R> {    // 接受输入参数,对输入执行所需操作后  返回一个结果。    R apply(T t);    // 返回一个 先执行before函数对象apply方法,再执行当前函数对象apply方法的 函数对象。    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {       Objects.requireNonNull(before);       return (V v) -> apply(before.apply(v));    }    // 返回一个 先执行当前函数对象apply方法, 再执行after函数对象apply方法的 函数对象。    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {        Objects.requireNonNull(after);        return (T t) -> after.apply(apply(t));    }       // 返回一个执行了apply()方法之后只会返回输入参数的函数对象。    static <T> Function<T, T> identity() {        return t -> t;    } }

例:apply方法使用

public class FunctionDemo {    static void modifyTheValue(int valueToBeOperated, Function<Integer, Integer> function) {        int newValue = function.apply(valueToBeOperated);        /*         * Do some operations using the new value.         */        System.out.println(newValue);    }    public static void main(String[] args) {        int incr = 20;        int myNumber = 10;        // Function接口的实现  支持lambda表达式。        modifyTheValue(myNumber, (x)-> x + incr);        //  匿名内部类实现        modifyTheValue(myNumber, new Function<Integer, Integer>() {            @Override            public Integer apply(Integer t) {                return t + incr;            }        });    }}

例:andThen方法使用

    public static Integer modifyTheValue2(int value, Function<Integer, Integer> function1, Function<Integer, Integer> function2){         return  function1.andThen(function2).apply(value);    }    public static void main(String[] args) {        System.out.println(modifyTheValue2(3, val -> val + 2, val -> val + 3));    }

二、Consumer接口

代表了接受一个输入参数并且无返回的操作

例:accept方法使用

    public static void modifyTheValue3(int value, Consumer<Integer> consumer) {        consumer.accept(value);    }    public static void main(String[] args) {        modifyTheValue3(3, (x) -> System.out.println(x * 2));    }

输出:

6

三、Predicate接口

接受一个输入参数,返回一个布尔值结果。

例:test方法使用1

    public static boolean predicateTest(int value, Predicate<Integer> predicate) {        return predicate.test(value);    }    public static void main(String[] args) {        System.out.println(predicateTest(3, (x) -> x == 3));    }

输出:

true

例:test方法使用2

    public static void eval(List<Integer> list, Predicate<Integer> predicate) {        for (Integer n : list) {            if (predicate.test(n)) {                System.out.print(n + " ");            }        }//      list.forEach(n -> {//          if (predicate.test(n)) {//              System.out.print(n + " ");//          }//      });    }    public static void main(String args[]) {        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);        // Predicate<Integer> predicate = n -> true        // n 是一个参数传递到 Predicate 接口的 test 方法        // n 如果存在则 test 方法返回 true        System.out.println("输出所有数据:");        // 传递参数 n        eval(list, n -> true);        // Predicate<Integer> predicate1 = n -> n%2 == 0        // n 是一个参数传递到 Predicate 接口的 test 方法        // 如果 n%2 为 0 test 方法返回 true        System.out.println("\n输出所有偶数:");        eval(list, n -> n % 2 == 0);        // Predicate<Integer> predicate2 = n -> n > 3        // n 是一个参数传递到 Predicate 接口的 test 方法        // 如果 n 大于 3 test 方法返回 true        System.out.println("\n输出大于 3 的所有数字:");        eval(list, n -> n > 3);    }

输出:

输出所有数据:1 2 3 4 5 6 7 8 9 输出所有偶数:2 4 6 8 输出大于 3 的所有数字:4 5 6 7 8 9 

例:test方法使用3

    public static boolean validInput(String name, Predicate<String> function) {          return function.test(name);      }      public static void main(String args[]) {        String name = "冷冷";        if(validInput(name, s -> !s.isEmpty() &&  s.length() <= 3 )) {            System.out.println("名字输入正确");        }    }

三、Supplier接口

无参数,返回一个结果。

例:get方法使用

    public static String supplierTest(Supplier<String> supplier) {          return supplier.get();      }      public static void main(String args[]) {        String name = "冷冷";        System.out.println(supplierTest(() -> name.length() + ""));    }

输出:

2
原创粉丝点击