深入学习java源码之Operator.apply()与Operator.andThen()
函数式接口
概述:接口中只有一个抽象方法
下面介绍的可能很抽象,理解不了,至少在我看来单独的这几个借口是没有用的,跟最下面说的 Stream流一起用才会有效果
函数式接口,即适用于函数式编程场景的接口。而Java中的函数式编程体现就是Lambda,所以函数式接口就是可
以适用于Lambda使用的接口。只有确保接口中有且仅有一个抽象方法,Java中的Lambda才能顺利地进行推导。
备注:“语法糖”是指使用更加方便,但是原理不变的代码语法。例如在遍历集合时使用的for-each语法,其实
底层的实现原理仍然是迭代器,这便是“语法糖”。从应用层面来讲,Java中的Lambda可以被当做是匿名内部
类的“语法糖”,但是二者在原理上是不同的。
格式:
-
只要确保接口中有且仅有一个抽象方法即可:
修饰符 interface 接口名称 {
public abstract 返回值类型 方法名称(可选参数信息);
// 其他非抽象方法内容
}
-
@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 Type | Method and Description |
---|---|
static <T> BinaryOperator<T> | maxBy(Comparator<? super T> comparator) 返回一个 |
static <T> BinaryOperator<T> | minBy(Comparator<? super T> 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 Type | Method and Description |
---|---|
double | applyAsDouble(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 Type | Method and Description |
---|---|
default DoubleUnaryOperator | andThen(DoubleUnaryOperator after) 返回一个组合运算符,首先将该运算符应用于其输入,然后将 |
double | applyAsDouble(double operand) 将此运算符应用于给定的操作数。 |
default DoubleUnaryOperator | compose(DoubleUnaryOperator before) 返回一个组合运算符,首先将 |
static DoubleUnaryOperator | identity() 返回始终返回其输入参数的一元运算符。 |
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 Type | Method and Description |
---|---|
int | applyAsInt(int left, int right) 将此运算符应用于给定的操作数。 |
package java.util.function;
@FunctionalInterface
public interface IntBinaryOperator {
int applyAsInt(int left, int right);
}
Modifier and Type | Method and Description |
---|---|
default IntUnaryOperator | andThen(IntUnaryOperator after) 返回一个 |
int | applyAsInt(int operand) 将此运算符应用于给定的操作数。 |
default IntUnaryOperator | compose(IntUnaryOperator before) 返回一个 |
static IntUnaryOperator | identity() 返回始终返回其输入参数的一元运算符。 |
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 Type | Method and Description |
---|---|
long | applyAsLong(long left, long right) 将此运算符应用于给定的操作数。 |
package java.util.function;
@FunctionalInterface
public interface LongBinaryOperator {
long applyAsLong(long left, long right);
}
Modifier and Type | Method and Description |
---|---|
default LongUnaryOperator | andThen(LongUnaryOperator after) 返回一个组合运算符,首先将该运算符应用于其输入,然后将 |
long | applyAsLong(long operand) 将此运算符应用于给定的操作数。 |
default LongUnaryOperator | compose(LongUnaryOperator before) 返回一个组合运算符,首先将 |
static LongUnaryOperator | identity() 返回始终返回其输入参数的一元运算符。 |
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 Type | Method 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;
}
}