目录
一、函数式编程接口
Java中的函数式接口一共有四种,其都被注解@FunctionalInterface注释,表明这是一个函数式接口:
- 消费型接口(Consumer<T>):接口方法void accept(T t),参数T,无返回值;
- 供给型接口(Supplier<T>):接口方法T get(),返回T类型返回值;
- 函数型接口(Function<T, R>):接口方法R apply(T t),入参为T类型,返回R类型返回值;
- 断言型接口(Predicate<T>):接口方法boolean test(T t),入参为T类型,返回boolean值。
1.Consumer<T>接口
1.1 典型一入参接口Consumer<T>
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); };
}
}
示例:
Consumer<Integer> con =
(integer) -> System.out.println("Integer Value" + integer);
con.accept(2);
1.2 二入参接口BiConsumer<T, U>
BiConsumer接口源码:
@FunctionalInterface
public interface BiConsumer<T, U> {
void accept(T t, U u);
default BiConsumer<T, U> andThen(
BiConsumer<? super T, ? super U> after) {
Objects.requireNonNull(after);
return (l, r) -> {
accept(l, r);
after.accept(l, r);
};
}
}
示例:
BiConsumer<Float, Double> consumer =
(f, d) -> System.out.println(f.doubleValue() + d);
consumer.accept(1.5f, 5.55);
2.Supplier<T>接口
Supplier接口源码:
@FunctionalInterface
public interface Supplier<T> {
T get();
}
示例:
Supplier<Long> currentTimeSupplier = System::currentTimeMillis;
Long currentTime = currentTimeSupplier.get();
3.Function<T, R>接口
3.1 典型一入参接口Function<T, R>
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;
}
}
示例:
Function<Integer, Long> sqrtFunction =
(integer) -> new Double(Math.sqrt(integer)).longValue();
Long sqrtResult = sqrtFunction.apply(4);
3.2 二入参接口BiFunction<T, U, R>
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));
}
}
示例:
BiFunction<Float, Double, Long> toLong =
(f,d) -> f.longValue() + d.longValue();
System.out.println(toLong.apply(0.5f, 2.3));
4.Predicate<T>接口
4.1 典型一入参接口Predicate<T>
Predicate接口源码:
@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);
}
}
示例:
Predicate<Integer> isEven = (integer) -> integer % 2 == 0;
if (isEven.test(2)) {
System.out.println("这是偶数");
}
4.2 二入参接口BiPredicate<T, U>
BiPredicate接口源码:
@FunctionalInterface
public interface BiPredicate<T, U> {
boolean test(T t, U u);
default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) {
Objects.requireNonNull(other);
return (T t, U u) -> test(t, u) && other.test(t, u);
}
default BiPredicate<T, U> negate() {
return (T t, U u) -> !test(t, u);
}
default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
Objects.requireNonNull(other);
return (T t, U u) -> test(t, u) || other.test(t, u);
}
}
示例:
BiPredicate<Float, Double> isEquals = (f, d) -> d.equals(f.doubleValue());
if (isEquals.test(0.5f, 0.5)) {
System.out.println("float和double相等");
}