函數式接口定義及應用
隻包含一個抽象方法的接口,允許有預設實作方法和靜态實作方法的接口稱為函數式接口。
可以在函數式接口上使用
@FunctionalInterface
注解,标注它是一個函數式接口,同時javadoc也會包
含一條聲明,說明這個接口是一個函數式接口。
如果被
@FunctionalInterface
标注的接口不是一個函數式接口,編譯器将傳回一個提示原因的錯誤。
@FunctionalInterface
public interface ICalculator<M, N, R> {
/**
* 函數式接口隻有一個抽象方法
*/
R calculate(M m, N n);
/**
* 函數式接口可以有實作的預設方法
*/
default void defaultPrint(R result){
PrintUtil.println("預設方法輸出結果:" + result);
}
/**
* 函數式接口可以有實作的靜态方法
*/
static void staticPrint(String result){
PrintUtil.println("靜态方法輸出結果:" + result);
}
}
public class CalculatorDemo {
public static void main(String[] args) {
ICalculator<Integer, Integer, Integer> calculator = (x, y) -> x * 10 + y;
// 執行函數式接口ICalculator的calculate方法
Integer result = calculator.calculate(1, 2);
// 執行函數式接口的預設方法
calculator.defaultPrint(result);
// 執行函數式接口的靜态方法
ICalculator.staticPrint(result.toString());
}
}
核心函數式接口
函數式接口 | 參數類型 | 傳回類型 | 描述 |
---|---|---|---|
Consumer | T | void | 對類型為T的對象應用操作。 |
Supplier | 無 | 傳回類型為T的對象。 | |
Function | R | 對類型為T的對象應用操作,并R類型的傳回結果。 | |
Predicate | boolean | 确定類型為T的對象是否滿足限制條件,并傳回boolean類型的資料。 |
Consumer接口
Consumer接口源碼
java.util.function.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接口用例
public class ConsumerDemo {
public static void main(String[] args) {
String product = "産品";
Consumer<String> consumer1 = str -> PrintUtil.println(str.concat("被消費了"));
consumer1.accept(product);
Consumer<String> consumer2 = str -> PrintUtil.print(str.concat("已售空,無法消費"));
// 先執行對象consumer1的accept方法,在執行consumer2的accept方法
consumer1.andThen(consumer2).accept(product);
}
}
Supplier接口
Supplier接口源碼
java.util.function.Supplier
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Supplier接口用例
public class SupplierDemo {
public static void main(String[] args) {
Supplier<Integer> supplier = () -> new Random().nextInt(100);
PrintUtil.println("生成100以内随機數:" + supplier.get());
}
}
Function接口
Function接口源碼
java.util.function.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接口用例
public class FunctionDemo {
public static void main(String[] args) {
Function<Integer, String> function = x -> "生成指定範圍内的随機數:" + new Random().nextInt(x);
PrintUtil.println(function.apply(10));
}
}
Predicate接口
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
: targetRef::equals;
}
}
Predicate接口用例
原始類型特化函數式接口
函數描述符 | 原始類型特化 | |
---|---|---|
Predicate<T> | T->boolean | IntPredicate, LongPredicate, DoublePredicate |
Consumer<T> | T->void | IntConsumer, LongConsumer, DoubleConsumer |
Function<T,R> | T->R | IntFunction<R>, IntToDoubleFunction, IntToLongFunction, LongFunction<R>, LongToDoubleFunction, LongToIntFunction, DoubleFunction<R>, ToIntFunction<T>, ToDoubleFunction<T>, ToLongFunction<T> |
Supplier<T> | ()->T | BooleanSupplier, IntSupplier, LongSupplier, DoubleSupplier |
UnaryOperator<T> | T->T | IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator |
BinaryOperator<T> | (T,T)->T | IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator |
BiPredicate<L,R> | (L,R)->boolean | |
BiConsumer<T,U> | (T,U)->void | ObjIntConsumer<T>, ObjLongConsumer<T>, ObjDoubleConsumer<T> |
BiFunction<T,U,R> | (T,U)->R | ToIntBiFunction<T,U>, ToLongBiFunction<T,U>, ToDoubleBiFunction<T,U> |