使用Lambda表达式 依赖于函数接口
1.在接口中只能够允许有一个抽象方法
2.在函数接口中定义object类中方法
3.使用默认或者静态方法
4.@FunctionalInterface 表示该接口为函数接口
Lambda表达式的规范
函数接口的定义:在该接口中只能存在一个抽象方法,该接口称作为函数接口
JDK中自带的函数接口:
java.lang.Runnable
java.util.concurrent.Callable
java.security.PrivilegedAction
java.util.Comparator
java.io.FileFilter
java.nio.file.PathMatcher
java.lang.reflect.InvocationHandler
java.beans.PropertyChangeListener
java.awt.event.ActionListener
javax.swing.event.ChangeListener
我们也可以使用@FunctionalInterface修饰为函数接口
函数接口定义
1.在接口中只能有一个抽象方法
2.@FunctionalInterface 标记为该接口为函数接口
3.可以通过default 修饰为普通方法
4.可以定义object类中的方法
@FunctionalInterface
public interface MyFunctionalInterface {
void add();
default void get() {
}
String toString();
}
Java系统内置那些函数接口
消费型接口:
Conusmer
void accept(T t);
BiConusmer<T,U>
void accept(T t,U u);//增加一种入参类型
供给型接口
Supplier
void get();
函数型接口
Function<T ,R>
R apply(T t);
UnaryOperator
T apply(T t);//入参与返回值类型一致
BiFunction <T ,U,R>
R apply(T t,U u);//增加一个参数类型
BinaryOperator
T apply(T t1,T t2);//l两个相同类型入参与同类型返回值
ToIntFunction
ToLongFunction
ToDoubleFunction
IntFunction
LongFunction
DoubleFunction
断言型接口
Predicate
boolean test(T t);