一、JDK1.8常用函数式接口

  • Supplier接口 - 用来获取一个泛型参数指定类型的对 象数据

    1. Supplier接口
    2. @FunctionalInterface
    3. public interface Supplier<T> {
    4. T get();
    5. }
  • Consumer接口 - 正好与Supplier接口相反,它不是生产一个数据,而是消费一个数据, 其数据类型由泛型决定。

    1. @FunctionalInterface
    2. public interface Consumer<T> {
    3. void accept(T t);
    4. default Consumer<T> andThen(Consumer<? super T> after) {
    5. Objects.requireNonNull(after);
    6. return (T t) -> { accept(t); after.accept(t); };
    7. }
    8. }
  • Predicate接口 - 对某种类型的数据进行判断,从而得到一个boolean值结果。 ```java @FunctionalInterface public interface Predicate {

    boolean test(T t);

  1. default Predicate<T> and(Predicate<? super T> other) {
  2. Objects.requireNonNull(other);
  3. return (t) -> test(t) && other.test(t);
  4. }
  5. default Predicate<T> negate() {
  6. return (t) -> !test(t);
  7. }
  8. default Predicate<T> or(Predicate<? super T> other) {
  9. Objects.requireNonNull(other);
  10. return (t) -> test(t) || other.test(t);
  11. }
  12. static <T> Predicate<T> isEqual(Object targetRef) {
  13. return (null == targetRef)
  14. ? Objects::isNull
  15. : object -> targetRef.equals(object);
  16. }

}

  1. - **Function接口 - 来根据一个类型的数据得到另一个类型的数据,前者称为前置条件, 后者称为后置条件。**
  2. ```java
  3. @FunctionalInterface
  4. public interface Function<T, R> {
  5. R apply(T t);
  6. default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
  7. Objects.requireNonNull(before);
  8. return (V v) -> apply(before.apply(v));
  9. }
  10. default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
  11. Objects.requireNonNull(after);
  12. return (T t) -> after.apply(apply(t));
  13. }
  14. static <T> Function<T, T> identity() {
  15. return t -> t;
  16. }
  17. }

二、函数式接口应用

  • Supplier get方法应用 ```java public static String getString(Supplier supplier){ return supplier.get(); } public static void main(String[] args) { String str = getString(() -> {
    1. return "hello";
    }); System.out.println(str); }

— 结果 hello

  1. - **Consumer accept接口**
  2. - **Consumer andThen方法相当于调用两次accept**
  3. ```java
  4. public static void accept(String str, Consumer<String> consumer){
  5. consumer.accept(str);
  6. }
  7. public static void main(String[] args) {
  8. accept("hello world", (str) -> {
  9. String[] strArr =str.split(" ");
  10. System.out.println(strArr[0]);
  11. });
  12. }
  13. --- 输出结果 hello
  14. public static void acceptAndThen(String str, Consumer<String> consumer1, Consumer<String> consumer2){
  15. consumer1.andThen(consumer2).accept(str);
  16. }
  17. public static void main(String[] args) {
  18. acceptAndThen("hello world",
  19. (String str1) -> {
  20. String a = str1 + " a";
  21. System.out.println(a);
  22. },
  23. (String str2) -> {
  24. String b = str2 + " b";
  25. System.out.println(b);
  26. });
  27. }
  28. -- 输出结果
  29. hello world a
  30. hello world b
  • Predicate test方法
  • Predicate and方法,两个test的逻辑与 &&
  • Predicate or方法,两个test的或 ||
  • Predicate negate方法,对test方法进行去反 ```java public class Demo15PredicateTest {
    private static void method(Predicate predicate) {
    1. boolean veryLong = predicate.test("HelloWorld");
    2. System.out.println("字符串很长吗:" + veryLong);
    }
    public static void main(String[] args) {
    1. method(s ‐> s.length() > 5);
    } }

private static void method(Predicate one, Predicate two) { boolean isValid = one.and(two).test(“Helloworld”);
System.out.println(“字符串符合要求吗:” + isValid);
}
public static void main(String[] args) {
method(s ‐> s.contains(“H”), s ‐> s.contains(“W”));
}

  1. - **Function apply接口**
  2. Function 接口中主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。
  3. ```java
  4. public class Demo11FunctionApply {
  5. private static void method(Function<String, Integer> function) {
  6. int num = function.apply("10");
  7. System.out.println(num + 20);
  8. }
  9. public static void main(String[] args) {
  10. method(s ‐> Integer.parseInt(s));
  11. }
  12. }