深入学习java源码之Operator.apply()与Operator.andThen()

深入学习java源码之Operator.apply()与Operator.andThen()

函数式接口

概述:接口中只有一个抽象方法

下面介绍的可能很抽象,理解不了,至少在我看来单独的这几个借口是没有用的,跟最下面说的 Stream流一起用才会有效果

函数式接口,即适用于函数式编程场景的接口。而Java中的函数式编程体现就是Lambda,所以函数式接口就是可
以适用于Lambda使用的接口。只有确保接口中有且仅有一个抽象方法,Java中的Lambda才能顺利地进行推导。

备注:“语法糖”是指使用更加方便,但是原理不变的代码语法。例如在遍历集合时使用的for-each语法,其实
底层的实现原理仍然是迭代器,这便是“语法糖”。从应用层面来讲,Java中的Lambda可以被当做是匿名内部
类的“语法糖”,但是二者在原理上是不同的。

格式:

  1. 只要确保接口中有且仅有一个抽象方法即可:

 修饰符 interface 接口名称 {
    public abstract 返回值类型 方法名称(可选参数信息);
    // 其他非抽象方法内容
 }
  1. @FunctionalInterface注解

    与@Override 注解的作用类似,Java 8中专门为函数式接口引入了一个新的注解: @FunctionalInterface 。该注
    解可用于一个接口的定义上,一旦使用该注解来定义接口,编译器将会强制检查该接口是否确实有且仅有一个抽象方法,否则将会报错。需要注
    意的是,即使不使用该注解,只要满足函数式接口的定义,这仍然是一个函数式接口,使用起来都一样。

lambda表达式: (参数列表)->{代码}

UnaryOperator<T>

对输入参数执行操作,并且输入参数与返回参数类型相同

apply(T t)

UnaryOperator<String> unaryOperator = greet -> greet + " Bob!";
System.out.println(unaryOperator.apply("Hello")); // Hello Jack!

andThen(Function<? super R,? extends V> after)

UnaryOperator<String> unaryOperator1 = greet -> greet + " Jack!";
String greet = unaryOperator.andThen(unaryOperator1).apply("Hello");
System.out.println(greet); // Hello Bob! Jack!

compose(Function<? super V,? extends T> before)

String greet = unaryOperator.compose(unaryOperator1).apply("Hello");
System.out.println(greet); // Hello Jack! Bob!

BinaryOperator<T>

提供两个参数,并且返回结果与输入参数类型一致的结果

apply(T t, U u)

BinaryOperator<String> binaryOperator = (flag, flag1) -> flag + flag1;
System.out.println(binaryOperator.apply("Hello ", "Jack!")); // Hello Jack!

andThen(Function<? super R,? extends V> after)

Function<String, String> function = a -> a + "!!!";
System.out.println(binaryOperator.andThen(function).apply("Hello", " Jack")); // Hello Jack!!!

DoubleBinaryOperator

提供两个double参数并且返回double结果

applyAsDouble(double left, double right)

DoubleBinaryOperator doubleBinaryOperator = (doub1, doub2) -> doub1
	+ doub2;
System.out.println(doubleBinaryOperator.applyAsDouble(1.1, 2.3)); // 3.4

DoubleUnaryOperator

提供单个double参数并且返回double结果

applyAsDouble(double operand)

DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
System.out.println(doubleUnaryOperator.applyAsDouble(2.6)); // 5.1

andThen(DoubleUnaryOperator after)

DoubleUnaryOperator doubleUnaryOperator1 = doub -> doub * 3;
double result = doubleUnaryOperator.andThen(doubleUnaryOperator1)
	.applyAsDouble(10); 
System.out.println(result); // (10 + 2.5) * 3 = 37.5

compose(DoubleUnaryOperator before)

double result = doubleUnaryOperator.compose(doubleUnaryOperator1)
	.applyAsDouble(10);
System.out.println(result); // 10 * 3 + 2.5 = 32.5

 

java源码

Modifier and TypeMethod and Description
static <T> BinaryOperator<T>maxBy(Comparator<? super T> comparator)

返回一个BinaryOperator ,它根据指定的Comparator返回两个元素中的较大Comparator

static <T> BinaryOperator<T>minBy(Comparator<? super T> comparator)

返回BinaryOperator返回根据指定的两个元件的较小的Comparator

package java.util.function;

import java.util.Objects;
import java.util.Comparator;

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

 

Modifier and TypeMethod and Description
doubleapplyAsDouble(double left, double right)

将此运算符应用于给定的操作数。

package java.util.function;

@FunctionalInterface
public interface DoubleBinaryOperator {
    /**
     * Applies this operator to the given operands.
     *
     * @param left the first operand
     * @param right the second operand
     * @return the operator result
     */
    double applyAsDouble(double left, double right);
}

 

Modifier and TypeMethod and Description
default DoubleUnaryOperatorandThen(DoubleUnaryOperator after)

返回一个组合运算符,首先将该运算符应用于其输入,然后将 after运算符应用于结果。

doubleapplyAsDouble(double operand)

将此运算符应用于给定的操作数。

default DoubleUnaryOperatorcompose(DoubleUnaryOperator before)

返回一个组合运算符,首先将 before运算符应用于其输入,然后将该运算符应用于结果。

static DoubleUnaryOperatoridentity()

返回始终返回其输入参数的一元运算符。

package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface DoubleUnaryOperator {

    double applyAsDouble(double operand);

    default DoubleUnaryOperator compose(DoubleUnaryOperator before) {
        Objects.requireNonNull(before);
        return (double v) -> applyAsDouble(before.applyAsDouble(v));
    }

    default DoubleUnaryOperator andThen(DoubleUnaryOperator after) {
        Objects.requireNonNull(after);
        return (double t) -> after.applyAsDouble(applyAsDouble(t));
    }

    static DoubleUnaryOperator identity() {
        return t -> t;
    }
}

 

Modifier and TypeMethod and Description
intapplyAsInt(int left, int right)

将此运算符应用于给定的操作数。

package java.util.function;

@FunctionalInterface
public interface IntBinaryOperator {

    int applyAsInt(int left, int right);
}

 

Modifier and TypeMethod and Description
default IntUnaryOperatorandThen(IntUnaryOperator after)

返回一个 after运算符,首先将该运算符应用于其输入,然后将 after运算符应用于结果。

intapplyAsInt(int operand)

将此运算符应用于给定的操作数。

default IntUnaryOperatorcompose(IntUnaryOperator before)

返回一个 before运算符,首先将 before运算符应用于其输入,然后将该运算符应用于结果。

static IntUnaryOperatoridentity()

返回始终返回其输入参数的一元运算符。

package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface IntUnaryOperator {

    int applyAsInt(int operand);

    default IntUnaryOperator compose(IntUnaryOperator before) {
        Objects.requireNonNull(before);
        return (int v) -> applyAsInt(before.applyAsInt(v));
    }

    default IntUnaryOperator andThen(IntUnaryOperator after) {
        Objects.requireNonNull(after);
        return (int t) -> after.applyAsInt(applyAsInt(t));
    }

    static IntUnaryOperator identity() {
        return t -> t;
    }
}

 

Modifier and TypeMethod and Description
longapplyAsLong(long left, long right)

将此运算符应用于给定的操作数。

package java.util.function;

@FunctionalInterface
public interface LongBinaryOperator {

    long applyAsLong(long left, long right);
}

 

Modifier and TypeMethod and Description
default LongUnaryOperatorandThen(LongUnaryOperator after)

返回一个组合运算符,首先将该运算符应用于其输入,然后将 after运算符应用于结果。

longapplyAsLong(long operand)

将此运算符应用于给定的操作数。

default LongUnaryOperatorcompose(LongUnaryOperator before)

返回一个组合运算符,首先将 before运算符应用于其输入,然后将该运算符应用于结果。

static LongUnaryOperatoridentity()

返回始终返回其输入参数的一元运算符。

package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface LongUnaryOperator {

    long applyAsLong(long operand);

    default LongUnaryOperator compose(LongUnaryOperator before) {
        Objects.requireNonNull(before);
        return (long v) -> applyAsLong(before.applyAsLong(v));
    }

    default LongUnaryOperator andThen(LongUnaryOperator after) {
        Objects.requireNonNull(after);
        return (long t) -> after.applyAsLong(applyAsLong(t));
    }

    static LongUnaryOperator identity() {
        return t -> t;
    }
}

 

Modifier and TypeMethod and Description
static <T> UnaryOperator<T>identity()

返回始终返回其输入参数的一元运算符。

package java.util.function;

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

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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wespten

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值