Supplier

供给型函数

  1. @FunctionalInterface
  2. public interface Supplier<T> {
  3. /**
  4. * Gets a result.
  5. *
  6. * @return a result
  7. */
  8. T get();
  9. }

Consumer

消费型函数

  1. @FunctionalInterface
  2. public interface Consumer<T> {
  3. /**
  4. * Performs this operation on the given argument.
  5. *
  6. * @param t the input argument
  7. */
  8. void accept(T t);
  9. /**
  10. * Returns a composed {@code Consumer} that performs, in sequence, this
  11. * operation followed by the {@code after} operation. If performing either
  12. * operation throws an exception, it is relayed to the caller of the
  13. * composed operation. If performing this operation throws an exception,
  14. * the {@code after} operation will not be performed.
  15. *
  16. * @param after the operation to perform after this operation
  17. * @return a composed {@code Consumer} that performs in sequence this
  18. * operation followed by the {@code after} operation
  19. * @throws NullPointerException if {@code after} is null
  20. */
  21. default Consumer<T> andThen(Consumer<? super T> after) {
  22. Objects.requireNonNull(after);
  23. return (T t) -> { accept(t); after.accept(t); };
  24. }
  25. }

Runnable

无参无返回型函数

  1. @FunctionalInterface
  2. public interface Runnable {
  3. /**
  4. * When an object implementing interface <code>Runnable</code> is used
  5. * to create a thread, starting the thread causes the object's
  6. * <code>run</code> method to be called in that separately executing
  7. * thread.
  8. * <p>
  9. * The general contract of the method <code>run</code> is that it may
  10. * take any action whatsoever.
  11. *
  12. * @see java.lang.Thread#run()
  13. */
  14. public abstract void run();
  15. }

Function

有参有返回型函数

  1. @FunctionalInterface
  2. public interface Function<T, R> {
  3. /**
  4. * Applies this function to the given argument.
  5. *
  6. * @param t the function argument
  7. * @return the function result
  8. */
  9. R apply(T t);
  10. default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
  11. Objects.requireNonNull(before);
  12. return (V v) -> apply(before.apply(v));
  13. }
  14. default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
  15. Objects.requireNonNull(after);
  16. return (T t) -> after.apply(apply(t));
  17. }
  18. static <T> Function<T, T> identity() {
  19. return t -> t;
  20. }
  21. }

参考