java内置的4大核心函数式接口:

消费型接口 Consumer<T>       void accept(T t)

供给型接口 Supplier<T>           T get()

函数型接口 Function<T, R>      R apply(T t)

断定型接口 Predicate<T>        boolean test(T t) 

 

对应的demo:

public class FunctionInterfaceTest {
    public static void main(String[] args) {
        Consumer<String> consumer = str -> System.out.println(str);
        consumer.accept("hello");

        Supplier<String> supplier = () -> "hi";
        System.out.println(supplier.get());

        Function<Integer, String> function = num -> "str" + num;
        System.out.println(function.apply(12));

        Predicate<Integer> predicate = num -> num > 0 ? true : false;
        System.out.println(predicate.test(12));
    }
}

JDK8 - 四大函数式接口

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-11-17
  • 2021-12-29
  • 2021-11-07
猜你喜欢
  • 2022-12-23
  • 2023-02-03
  • 2021-09-05
  • 2021-06-26
  • 2021-12-25
  • 2021-08-21
相关资源
相似解决方案