Functional Interface | Function Descriptor | Specific Interface |
---|---|---|
Predicate | T -> boolean | IntPredicate LongPredicate DoublePredicate |
Consumer | T -> void | IntConsumer LongConsumer DoubleConsumer |
Function | T -> R | IntFunction<R> IntToLongFunction IntToDoubleFunction LongFunction<R> LongToIntFunction LongToDoubleFunction DoubleFunction<R> DoubleToIntFunction DoubleToLongFunction ToIntFunction<T> ToLongFunction<T> ToDoubleFunction<T> |
Supplier | () -> T | BooleanSupplier IntSupplier LongSupplier DoubleSupplier |
UnaryOperator | T -> T | IntUnaryOperator LongUnaryOperator DoubleUnaryOperator |
BinaryOperator | (T, T) -> T | IntBinaryOperator LongBinaryOperator DoubleBinaryOperator |
BiPredicate | (T, U) -> boolean | |
BiConsumer | (T, U) -> void | ObjIntConsumer<T> ObjLongConsumer<T> ObjDoubleConsumer<T> |
BiFunction | (T, U) -> R | ToIntBiFunction<T, U> ToLongBiFunction<T, U> ToDoubleBiFunction<T, U> |
inventory.sort(Comparator.comparing(Apple:getWeight)
.reversed()
.thenComparing(Apple::getCountry));
Predicate<Apple> notRedApple = redApple.negate();
Predicate<Apple> redAndHeavyAppleOrNotGreen =
redApple.and(apple -> apple.getWeight() > 150)
.or(apple -> GREEN.equals(a.getColor()));
IntUnaryOperator f = i -> i + 1;
IntUnaryOperator g = i -> i * 3;
System.out.println("f(x) = x + 1");
System.out.println("g(x) = x * 3");
System.out.printf("g(f(2)) = %d\n", f.andThen(g).applyAsInt(2)); // 9
System.out.printf("f(g(2)) = %d\n",f.compose(g).applyAsInt(2)); // 7