四大核心函数式接口
函数式接口 | 参数类型 | 返回类型 | 使用场景 |
---|---|---|---|
Consumer 消费型接口 | T | Void | 对类型为 T 的对象应用操作,接口定义的方法:void accept(T t) |
Supplier 供给型接口 | 无 | T | 返回类型为 T 的对象,接口定义的方法:T get() |
Function<T, R>函数式接口 | T | R | 对类型为 T 的对象应用操作,并 R 类型的返回结果。接口定义的方法:R apply(T t) |
Predicate 断言型接口 | T | boolean | 确定类型为 T 的对象是否满足约束条件,并返回boolean 类型的数据。接口定义的方法:boolean test(T t) |
其他函数接口
函数式接口 | 参数类型 | 返回类型 | 使用场景 |
---|---|---|---|
BiFunction(T, U, R) | T,U | R | 对类型为 T,U 的参数应用操作,返回 R 类型的结果。接口定义的方法:R apply(T t, U u) |
UnaryOperator(Function 子接口) | T | T | 对类型为 T 的对象进行一 元运算, 并返回 T 类型的 结果。 包含方法为T apply(T t) |
BinaryOperator(BiFunction 子接口) | T,T | T | 对类型为 T 的对象进行二 元运算, 并返回 T 类型的 结果。 包含方法为T apply(T t1, T t2) |
BiConsumer<T, U> | T,U | Void | 对类型为 T, U 参数应用 操作。 包含方法为 void accept(T t, U u) |
ToIntFunction | T | int | 计算 int 值的函数 |
ToLongFunction | T | long | 计算 long 值的函数 |
ToDoubleFunction | T | double | 计算 double 值的函数 |
IntFunction | int | R | 参数为 int 类型的函数 |
LongFunction | long | R | 参数为 long 类型的函数 |
DoubleFunction | double | R | 参数为 double 类型的函数 |
四大核心函数式接口应用举例
Consumer接口
Consumer 接口是消费性接口,无返回值。Java8 中对 Consumer 的定义如下所示。
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
使用示例
public static void handleConsumer(Consumer<Integer> consumer,Integer number){
consumer.accept(number);
}
@Test
public void testConsumer(){
handleConsumer(i-> System.out.println(i),100);
}
Supplier接口
Supplier 接口是供给型接口,有返回值,Java8 中对 Supplier 接口的定义如下所示。
@FunctionalInterface
public interface Supplier<T> {
T get();
}
使用示例
public static List<Integer> getList(int number, Supplier<Integer> supplier){
List<Integer> list = new ArrayList<>();
for (int i=0;i<number;i++){
list.add(supplier.get());
}
return list;
}
@Test
public void testSupplier(){
List<Integer> list = getList(10, () -> new Random().nextInt(100));
}
Function接口
Function 接口是函数型接口,有返回值,Java8 中对 Function 接口的定义如下所示。
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
使用示例
public static String dealString(String str, Function<String,String> function){
return function.apply(str);
}
@Test
public void testFunction(){
String str = dealString("hello",s -> s.toUpperCase());
System.out.println(str);
}
Predicate接口
Predicate 接口是断言型接口,返回值类型为 boolean,Java8 中对 Predicate 接口的定义如下
所示。
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
使用说明:
public static List<String> dealPredicate(List<String> list, Predicate<String> predicate){
List<String> stringList = new ArrayList<>();
for (String str:list){
if(predicate.test(str)){
stringList.add(str);
}
}
return stringList;
}
@Test
public void testPredicate(){
System.out.println(dealPredicate(Arrays.asList("hello","world","aaa","bbb"),s->s.length()>=5));
}
版权声明:本文为ntzzzsj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。