美文网首页Java
函数接口学习

函数接口学习

作者: 愤怒的老照 | 来源:发表于2020-05-21 17:40 被阅读0次

1、Consumer,相当于消费者,接收一个参数但是不返回。在stream中的最终运算使用到。

public interface Consumer<T> {
  // 消费一个数据
  void accept(T var1);

  // 链式调用,返回一个组合的Consumer,顺序执行
  default Consumer<T> andThen(Consumer<? super T> after) {
      Objects.requireNonNull(after);
      return (t) -> {
          this.accept(t);
          after.accept(t);
      };
  }
}
// accept方法使用,最后输出com www
Consumer<String> consumer = System.out::println;
ImmutableList.of("com","www").forEach(consumer);

// andThen方法使用,多个Consumer形成链式调用,先向list中添加,然后打印
Consumer<List<String>> consumer1 = list -> Collections.addAll(list,"com","www");;
Consumer<List<String>> consumer2 = list -> list.forEach(System.out::println);
consumer1.andThen(consumer2).accept(new ArrayList<>());

补充:IntConsumer、DoubleConsumer、LongConsumer不需要使用泛型

2、Supplier,相当于生产者,生产一个结果返回。stream的generate这类的函数有用到。

public interface Supplier<T> {
    // 只有一个get方法,用于生产数据
    T get();
}
Supplier<Double> supplier = () -> Math.random();
System.out.println(supplier.get());

3、Predicate,预测,接收一个参数,判断并返回true或者false,经常用做过滤一些东西,比如Stream的filter。因为是true/false,所以有与或非逻辑。

public interface Predicate<T> {
    // 判断返回true/false
    boolean test(T var1);

    // 接收一个Predicate,返回一个合并的Predicate,作用是&&
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> {
            return this.test(t) && other.test(t);
        };
    }

    // 取反
    default Predicate<T> negate() {
        return (t) -> {
            return !this.test(t);
        };
    }
    
    // 接收一个Predicate,返回一个合并的Predicate,作用是||
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> {
            return this.test(t) || other.test(t);
        };
    }
}

// 用作filter的判断
Predicate<String> predicate = item->item.startsWith("www");

ImmutableList.of("www","xxx","wwwccc","fff").stream().filter(predicate).collect(Collectors.toList());

// 定义两个过滤条件,返回并集。这种需求之前遇到过,现在才知道有这种实现方式
 Predicate<String> predicate = item -> item.startsWith("www");
 Predicate<String> predicate1 = item -> item.length() == 3;
System.out.println(ImmutableList.of("www", "xxx", "wwwccc", "fff").stream().filter(predicate.or(predicate1)).collect(Collectors.toList()));
    

4、Function,接收一个参数T,返回一个参数F。stream的map中有用到

 // apply
 R apply(T var1);

   // 合并,可以实现链式调用,先执行参数
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (v) -> {
            return this.apply(before.apply(v));
        };
    }

    // 链式调用,先执行调用者
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (t) -> {
            return after.apply(this.apply(t));
        };
    }

   // 返回本身,还是很有用的,可以用Function::identity代替it -> it;
    static <T> Function<T, T> identity() {
        return (t) -> {
            return t;
        };
    }
 // function 在map的用法
 Function<String, Integer> function = (str) -> Integer.parseInt(str);                
 ImmutableList.of("1","2","3").stream().map(function).collect(Collectors.toList());  

// funciton 的链式调用,先转成int在+1
Function<String, Integer> function = (str) -> Integer.parseInt(str);                                  
Function<Integer, Integer> function1 = item -> item + 1;                                              
ImmutableList.of("1","2","3").stream().map(function.andThen(function1)).collect(Collectors.toList()); 

5、BiFunction,这是Function的增强版,Function只能接收一个参数,如果想要接收多个参数,可以使用BiFunction,与之类似的还有BiConsumer

public interface BiFunction<T, U, R> {
    R apply(T var1, U var2);

    default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (t, u) -> {
            return after.apply(this.apply(t, u));
        };
    }
}
BiFunction<Integer, Integer, String> biFunction = (a, b) -> String.valueOf(a + b); 
Function<String, String> function = (c) -> "Hello:" + c;                           

一些方法用stream也可以处理的很好,而且可读性更强

相关文章

  • 函数接口学习

    1、Consumer,相当于消费者,接收一个参数但是不返回。在stream中的最终运算使用到。 补充:IntCon...

  • java基础-day23-函数式接口和Stream流

    函数式接口和Stream 1. 函数式接口 1.1 函数式接口概述 1.2 常用函数式接口 1.3 比较器函数式接...

  • 函数式接口

    函数式接口 声明:java8新特性系列为个人学习笔记,参考地址点击这里,侵删!! 函数式接口(Functional...

  • 2020-07-04【函数式接口】

    函数式接口概述 函数式接口作为方法的参数 函数式接口作为方法的返回值 常见的函数式接口 Supplier接口 Co...

  • Android学习Kotlin之五、对象-接口-抽象类

    对象-接口-抽象 其它Kotlin文章Android学习Kotlin之一、常量-条件-函数-高阶函数[https:...

  • lambda表达式

    导图 文章最后有源码 简介 学习lambda表达式就要先知道函数式接口是什么?函数式接口(Functional I...

  • 随笔:Innodb truncate内存维护代价高于drop

    本文为随笔。留个接口后面好详细学习。 函数接口 buf_LRU_flush_or_remove_pages 用于确...

  • 12.函数式接口

    主要内容 自定义函数式接口 函数式编程 常用函数式接口 第一章 函数式接口 1.1 概念 函数式接口在Java中是...

  • Kotlin学习笔记:类和接口

    Kotlin学习笔记:概述Kotlin学习笔记:基本语法和函数Kotlin学习笔记:类和接口Kotlin学习笔记:...

  • Kotlin学习笔记:概述

    Kotlin学习笔记:概述Kotlin学习笔记:基本语法和函数Kotlin学习笔记:类和接口Kotlin学习笔记:...

网友评论

    本文标题:函数接口学习

    本文链接:https://www.haomeiwen.com/subject/yictahtx.html