ITPub博客

首页 > 应用开发 > Java > 关于jdk8中的四大核心函数式接口

关于jdk8中的四大核心函数式接口

原创 Java 作者:8563084 时间:2022-03-12 17:41:45 0 删除 编辑

一、Consumer 接口

1、接口说明

Consumer 接口是消费性接口,无返回值。Java8 中对 Consumer 的定义如下所示。

@FunctionalInterfacepublic interface Consumer<T> {
    void accept(T t);
    
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }}

2、使用示例


public void handlerConsumer(Integer number, Consumer<Integer> consumer){
    consumer.accept(number);}@Testpublic void test1(){
    this.handlerConsumer(10000, (i) -> System.out.println(i));}

二、Supplier 接口

1、接口说明

Supplier 接口是供给型接口,有返回值,Java8 中对 Supplier 接口的定义如下所示。

@FunctionalInterfacepublic interface Supplier<T> {
    T get();}

2、使用示例

public List<Integer> getNumberList(int num, Supplier<Integer> supplier){
    List<Integer> list = new ArrayList<>();
    for(int i = 0; i < num; i++){
        list.add(supplier.get())
    }
    return list;}@Testpublic void test2(){
    List<Integer> numberList = this.getNumberList(10, () -> new Random().nextInt(100));
    numberList.stream().forEach(System.out::println);}

三、Function 接口

1、接口说明

Function 接口是函数型接口,有返回值,Java8 中对 Function 接口的定义如下所示。

@FunctionalInterfacepublic interface Function<T, R> {
    
    R apply(T t);
    /**
     * 先执行参数(即也是一个Function)的,再执行调用者(同样是一个Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }
    /**
     * 先执行调用者,再执行参数,和compose相反。
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
   /**
     * 返回当前正在执行的方法
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }}

2、使用示例

public String handlerString(String str, Function<String, String> func){
    return func.apply(str);}@Testpublic void test3(){
    String str = this.handlerString("binghe", (s) -> s.toUpperCase());
    System.out.println(str);}

3、关于andThen的使用

Function<Integer, Integer> functionA = i -> i * 2;Function<Integer, String> functionB = i -> "-" + i;functionA.andThen(functionB).apply(2)输出结果是:-4等价于下面的functionCFunction<Integer, Integer> functionA = i -> i * 2;Function<Integer, String> functionB = i -> "-" + i;Function<Integer, String> functionC = t -> functionB.apply(functionA.apply(t));输出结果也是:-4

4、对比下几个方法的使用

    public static void main(String[] args) {
        Function<Integer, Integer> times2 = i -> i*2;
        Function<Integer, Integer> squared = i -> i*i;
        //8
        System.out.println(times2.apply(4));
        //16
        System.out.println(squared.apply(4));
        
        //32                先4×4然后16×2,先执行apply(4),在times2的apply(16),先执行参数,再执行调用者。
        System.out.println(times2.compose(squared).apply(4));  
        
        //64               先4×2,然后8×8,先执行times2的函数,在执行squared的函数。
        System.out.println(times2.andThen(squared).apply(4));  
        
         //16
        System.out.println(Function.identity().compose(squared).apply(4));  
    }

四、Predicate 接口

1、接口说明

Predicate 接口是断言型接口,返回值类型为 boolean,Java8 中对 Predicate 接口的定义如下所示。

@FunctionalInterfacepublic interface 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> negate() {
        return (t) -> !test(t);
    }
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }}

2、使用示例

public List<String> filterString(List<String> list, Predicate<String> predicate){
    List<String> strList = new ArrayList<>();
    for(String str : list){
        if(predicate.test(str)){
            strList.add(str);
        }
    }
    return strList;}@Testpublic void test4(){
    List<String> list = Arrays.asList("Hello", "Lambda", "binghe", "lyz", "World");
    List<String> strList = this.filterString(list, (s) -> s.length() >= 5);
    strList.stream().forEach(System.out::println);}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69989885/viewspace-2869874/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: Tomcat异常讲解
请登录后发表评论 登录
全部评论

注册时间:2020-12-02

  • 博文量
    27
  • 访问量
    61170