java8的四大函数式接口

前言

JDK1.8版本的java内置四大核心函数式接口,在java.util.function,可以使用lambda表示式。

函数式接口参数类型返回类型用途
Comsumer
消费型接口
Tvoid对类型为T的对象应用操作,包含方法:
void accept(T t)
Function<T, R>
函数型接口
TR对类型为T的对象应用操作,并返回结果。结果位R类型的对象。包含方法:
R apply(T t)
Predicate
断定型接口
Tboolean确定类型为T的对象是否满足某约束,并返回boolean值。包含方法:
boolean test(T t)
Supplier
供给型接口
T返回类型为T的对象,包含方法:
T get()

2、Consumer

消费类型,即只有支出没有收入(有入参,没有返回值)

  • 源码
@FunctionalInterface
public 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); };
  }
}
  • 代码示例 1
public static void main(String[] args){
  List<String> list = Lists.newArrayList();
  Consumer<String> consumer = s-> {
    if(StringUtils.isNotEmpty(s)){
      list.add(s);
    }
  }
  consumer.aceept("test");
  log.info("====={}",list);
}
// 输出结果:=====[test]
  • 代码示例 2
public static void main(String[] args) {
    List<String> list = Lists.newArrayList();
    Consumer<String> consumer = s-> {
      if(StringUtils.isNotEmpty(s)){
        list.add(s);
      }
    };
    consumer = consumer.andThen(s->{
      if("test".equals(s)){
        list.add("test521");
      }
    });
    consumer.accept("test");
    log.info("====={}",list);
  }
// 输出结果:=====[test, test521]

3、Function

函数类型,有入参也有返回值。

  • 源码
@FunctionalInterface
public interface Function<T, R> {
  R apply(T t);
  default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    Objects.requireNonNull(before);
    return (V v) -> apply(before.apply(v));
  }
  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;
  }
}
  • 代码示例 1
public static void main(String[] args){
  Function<Integer, Integer> function = x-> x+1;
  System.out.println(function.apply(10));
}
// 输出结果:11
  • 代码示例 2
public static void main(String[] args){
  Function<Integer, Integer> function = x-> x+1;
  Function<Integer, Integer> composeF = x-> x*2;
  System.out.println(function.compose(composeF).apply(10));
}
// 输出结果:21
// 执行逻辑:先执行 composeF,再执行 function
  • 代码示例 3
public static void main(String[] args){
  Function<Integer, Integer> function = x-> x+1;
  Function<Integer, Integer> andThenF = x-> x*2;
  System.out.println(function.andThen(andThenF).apply(10));
}
// 输出结果:22
// 执行逻辑:先执行 function,再执行 andThenF
  • 代码示例 4
  public static void main(String[] args){
    System.out.println(Function.identity().apply("我只会把参数当做返回值"));
  }
  // 输出结果:我只会把参数当做返回值

4、Predicate

断定类型,既有参数,也有返回值,但是返回值只能是boolean

  • 源码
@FunctionalInterface
public 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);
    }

    @SuppressWarnings("unchecked")
    static <T> Predicate<T> not(Predicate<? super T> target) {
        Objects.requireNonNull(target);
        return (Predicate<T>)target.negate();
    }
}
  • 代码示例 1
public static void main(String[] args){
  Predicate<Integer> predicate = x->x%2==0;
  System.out.println(predicate.test(99));
  // 输出结果:false
  System.out.println(predicate.test(100));
  // 输出结果:true
}

代码示例 2

public static void main(String[] args){
  Predicate<Integer> predicate = x->x%2==0;
  System.out.println(predicate.negate().test(9));
  // 输出结果:true
  System.out.println(predicate.negate().test(10));
  // 输出结果:false
  System.out.println(Stream.of(2, 3).filter(predicate.negate()).count());
  // 输出结果:1
}
  • 代码示例 3
public static void main(String[] args){
  Predicate<Integer> predicate = x->x%2==0;
  Predicate<Integer> andPredicate = x-> x > 10;
  System.out.println(predicate.and(andPredicate).test(8));
  // 输出结果:false
  System.out.println(predicate.and(andPredicate).test(9));
  // 输出结果:false
  System.out.println(predicate.and(andPredicate).test(11));
  // 输出结果:false
  System.out.println(predicate.and(andPredicate).test(12));
  // 输出结果:true
}
  • 代码示例 4
public static void main(String[] args){
  Predicate<Integer> equalPredicate = Predicate.isEqual(5);
  System.out.println(equalPredicate.test(5));
  // 输出结果:true
  System.out.println(equalPredicate.test(6));
  // 输出结果:false
}

4、Supplier

供给类型,只有返回值,没有参数

  • 源码
@FunctionalInterface
public interface Supplier<T> {
  T get();
}
  • 代码示例
public static void main(String[] args){
  Supplier<String> supplier = ()->{
    return "我是测试数据";
  };
  
  System.out.println(supplier.get())
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值