四大函数式接口函数式接口只有一个方法的接口4大函数式接口：consumer，funtion，predicate，supplierFunction函数式接口package com.sw.function; import java.util.function.Function; /** * @Author suaxi * @Date 2021/2/15 22:00 * Function函数式接口 */ public class Test01 { public static void main(String[] args) { // Function<String, String> function = new Function<String, String>() { // @Override // public String apply(String s) { // return s; // } // }; //lambda表达式简化 Function<String, String> function = (s) ->{ return s; }; System.out.println(function.apply("函数式接口测试")); } } Predicate断定型接口package com.sw.function; import java.util.function.Predicate; /** * @Author suaxi * @Date 2021/2/15 22:09 * Predicate断定型接口,返回值为boolean类型 */ public class PredicateTest { public static void main(String[] args) { // Predicate<String> predicate = new Predicate<String>() { // @Override // public boolean test(String s) { // return s.isEmpty(); // } // }; Predicate<String> predicate = (str) ->{ return str.isEmpty(); }; System.out.println(predicate.test("abc")); } } Consummer消费型接口package com.sw.function; import java.util.function.Consumer; /** * @Author suaxi * @Date 2021/2/15 22:17 * Consummer消费型接口，只有输入参数，没有返回值 */ public class ConsumerTest { public static void main(String[] args) { // Consumer<String> consumer = new Consumer<String>() { // @Override // public void accept(String s) { // System.out.println(s); // } // }; Consumer<String> consumer = (str) ->{ System.out.println(str); return; }; consumer.accept("消费型接口测试"); } } Supplier供给型接口package com.sw.function; import java.util.function.Supplier; /** * @Author suaxi * @Date 2021/2/15 22:25 * Supplier 供给型接口 没有输入参数，只有返回值 */ public class SupplierTest { public static void main(String[] args) { // Supplier<String> supplier = new Supplier<String>() { // @Override // public String get() { // return "你好，谢谢"; // } // }; Supplier<String> supplier = () -> { return "你好，谢谢"; }; System.out.println(supplier.get()); } }