Java 函数式接口

Java 函数式接口

概念

函数式接口在 Java 中是指:有且仅有一个抽象方法的接口

函数式接口可以被隐式转换为 Lambda 表达式

@FunctionalInterface

@FunctionalInterface // 标明为函数式接口
public interface MyFunctionInterface {
    void mrthod(); //抽象方法
}

一旦使用该注解来定义接口,编译器将会强制检查该接口是否确实有且仅有一个抽象方法,否则将会报错。需要注意的是,即使不使用该注解,只要满足函数式接口的定义,这仍然是一个函数式接口,使用起来都一样。(该接口是一个标记接口)

常用的函数式接口

Supplier 接口

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

用来返回一个值,即供应器

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); };
    }
}

用来消费一个值,即消费器

默认方法
  • andThen:组合多个 consumer,可以实现消费数据的时候,首先做一个操作,然后再做一个操作
Consumer<Integer> consumer = ((Consumer<Integer>) i -> System.out.println("a" + i))
    .andThen(i -> System.out.println("b" + i));

Stream.of(1, 2, 3, 4).forEach(consumer);
// 输出:a1 b1 a2 b2 a3 b3 a4 b4

Predicate 接口

@FunctionalInterface
public interface Predicate<T> {

    boolean test(T t);

    default Predicate<T> negate() {
        return (t) -> !test(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);
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

用来判断一个值是否符合条件,即判断器

默认方法
  • negate:取反
  • and:类似于 &&
  • or:类似于 ||
静态方法
  • isEqual:返回一个输入值是否等于目标值的 Predicate 接口

Function 接口

@FunctionalInterface
public interface Function<T, R> {

    R apply(T t);

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
    
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

用来将一个值转换成其它类型的值,即转换器

默认方法
  • andThen:组合多个 function,可以实现先做一个转换,再做一个转换
Function<Integer, Integer> mapper = ((Function<Integer, Integer>) i -> i * 10)
    .andThen(i -> i - 1);

Stream.of(1, 2, 3, 4)
    .map(mapper)
    .forEach(System.out::println);
// 输出:9 19 29 39
  • compose:也是组合多个 function,不过组合顺序跟 andThen 相反
Function<Integer, Integer> mapper = ((Function<Integer, Integer>) i -> i * 10)
    .compose(i -> i - 1);

Stream.of(1, 2, 3, 4)
    .map(mapper)
    .forEach(System.out::println);
// 输出:0 10 20 30
静态方法

identity:不做转换,原样返回输入值

UnaryOperator 接口

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {

    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

[[#Function 接口]]的特例,转换前和转换后的类型不变

BiFunction 接口

@FunctionalInterface
public interface BiFunction<T, U, R> {

    R apply(T t, U u);

    default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }
}

[[#Function 接口]]的另一个版本,接收两个值并返回一个新值

BinaryOperator 接口

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {

    public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
    }

    public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
    }
}

[[#BiFunction 接口]]的特例,转换前和转换后的类型不变

Java8 新增的函数式接口

Supplier

接口描述
Supplier<T>无参数,返回一个结果
BooleanSupplier代表了 boolean 值结果的提供方
IntSupplier无参数,返回一个 int 类型结果
LongSupplier无参数,返回一个结果 long 类型的值
DoubleSupplier代表一个 double 值结构的提供方

Consumer

接口描述
Consumer<T>代表了接受一个输入参数并且无返回的操作
IntConsumer接受一个 int 类型的输入参数,无返回值
LongConsumer接受一个 long 类型的输入参数,无返回值
DoubleConsumer代表一个接受 double 值参数的操作,并且不返回结果
ObjIntConsumer<T>接受一个 object 类型和一个 int 类型的输入参数,无返回值
BiConsumer<T,U>代表了一个接受两个输入参数的操作,并且不返回任何结果
ObjLongConsumer<T>接受一个 object 类型和一个 long 类型的输入参数,无返回值
ObjDoubleConsumer<T>接受一个 object 类型和一个 double 类型的输入参数,无返回值

Predicate

接口描述
Predicate<T>接受一个输入参数,返回一个布尔值结果
IntPredicate接受一个 int 输入参数,返回一个布尔值的结果
LongPredicate接受一个 long 输入参数,返回一个布尔值类型结果
DoublePredicate代表一个拥有 double 值参数的 boolean 值方法
BiPredicate<T,U>代表了一个两个参数的 boolean 值方法

Function

接口描述
Function<T,R>接受一个输入参数,返回一个结果
IntFunction<R>接受一个 int 类型输入参数,返回一个结果
IntToLongFunction接受一个 int 类型输入,返回一个 long 类型结果
IntToDoubleFunction接受一个 int 类型输入,返回一个 double 类型结果
LongFunction<R>接受一个 long 类型输入参数,返回一个结果
LongToIntFunction接受一个 long 类型输入,返回一个 int 类型结果
LongToDoubleFunction接受一个 long 类型输入,返回一个 double 类型结果
DoubleFunction<R>代表接受一个 double 值参数的方法,并且返回结果
DoubleToIntFunction接受一个 double 类型输入,返回一个 int 类型结果
DoubleToLongFunction接受一个 double 类型输入,返回一个 long 类型结果
ToIntFunction<T>接受一个输入参数,返回一个 int 类型结果
ToLongFunction<T>接受一个输入参数,返回一个 long 类型结果
ToDoubleFunction<T>接受一个输入参数,返回一个 double 类型结果

UnaryOperator

接口描述
UnaryOperator<T>接受一个参数为类型 T,返回值类型也为 T
IntUnaryOperator接受一个参数同为类型 int,返回值类型也为 int
LongUnaryOperator接受一个参数同为类型 long,返回值类型也为 long
DoubleUnaryOperator接受一个参数同为类型 double,返回值类型也为 double

BiFunction

接口描述
BiFunction<T,U,R>代表了一个接受两个输入参数的方法,并且返回一个结果
ToIntBiFunction<T,U>接受两个输入参数,返回一个 int 类型结果
ToLongBiFunction<T,U>接受两个输入参数,返回一个 long 类型结果
ToDoubleBiFunction<T,U>接受两个输入参数,返回一个 double 类型结果

BinaryOperator

接口描述
BinaryOperator<T>代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果
IntBinaryOperator接受两个参数同为类型 int,返回值类型也为 int
LongBinaryOperator接受两个参数同为类型 long,返回值类型也为 long
DoubleBinaryOperator代表了作用于两个 double 值操作符的操作,并且返回了一个 double 值的结果

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值