一、函数式接口
-
函数式接口的含义
只有一个方法的接口,就是函数式接口
如下面这些接口
@FunctionalInterface public interface Runnable { public abstract void run(); } @FunctionalInterface public interface Callable<V> { V call() throws Exception; }
函数式接口的意义是为了简化编程模型
-
Function 函数式接口
public class FunctionTest { 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 = (str)->{return str;}; System.out.println(function.apply("abcd1234")); } }
-
Predicate 函数式接口
public class PredicateTest { public static void main(String[] args) { // Predicate<String> predicate = new Predicate<String>() { // @Override // public boolean test(String o) { // return o.isEmpty(); // } // }; //Lmabda写法 Predicate<String> predicate = (str)->{return str.isEmpty();}; System.out.println(predicate.test("")); } }
-
Consumer 函数式接口
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 = (s)->{System.out.println(s);};
consumer.accept("fdfsd");
}
}
-
Supplier 函数式接口
public class SupplierTest { public static void main(String[] args) { // Supplier<Integer> supplier = new Supplier<Integer>() { // @Override // public Integer get() { // return 12345; // } // }; Supplier<Integer> supplier = ()->{return 12345;}; System.out.println(supplier.get()); } }