}
}
可以理解为一个比较器 常用方法 test() 返回一个boolean 值
negate() 取反比较(相比于test) 返回当前实例 可以链式调用
执行结果:
false
true
Function :
源码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Function<T, R> {
R apply(T var1);
default Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (v) -> {
return this.apply(before.apply(v));
};
}
default Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (t) -> {
return after.apply(this.apply(t));
};
}
static Function<T, T> identity() {
return (t) -> {
return t;
};
}
}
函数式操作,学习这个需要对泛型(java1.5)有一定的了解,比如:
CallBack
List<? super Android>
List<? extends Method>
List<? super Object>
Map<K,V>
apply() :
Function<Integer,Integer> function1 = integer -> integer*=3;
System.out.println(function1.apply(6));
输出结果:
18
compose:
private static void functionMethod() {
Function<Integer, Integer> function1 = integer -> integer *= 3;
System.out.println(function1.apply(6));
Function<Integer,Integer> function4 = integer -> integer *= 3;
System.out.println(function1.compose(function4).apply(6));
Function<String, String> function2 = (user) -> user.substring(0, user.length() - 1);
System.out.println(function2.apply(“那一年 风吹半夏”));
Function<Person, Boolean> function3 = (person) -> person.getName().isBlank();
}
输出:
18
54
那一年 风吹半
supplier:
源码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package java.util.function;
@FunctionalInterface
public interface Supplier {
T get();
}
可以看出只有一个get方法,可以理解为是一个抽象工厂模式,下面我们进行测试一下.
private static void supplier(){
Supplier sp = String::new;
System.out.println(sp.get());
Supplier personSupplier = Person::new;
Person person = personSupplier.get();
person.setName(“陆小凤”);
System.out.println(person.getName());
}
执行结果:
陆小凤
Consumer:
源码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Consumer {
void accept(T var1);
default Consumer andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (t) -> {
this.accept(t);
after.accept(t);
Java高频面试专题合集解析:
当然在这还有更多整理总结的Java进阶学习笔记和面试题未展示,其中囊括了Dubbo、Redis、Netty、zookeeper、Spring cloud、分布式、高并发等架构资料和完整的Java架构学习进阶导图!
更多Java架构进阶资料展示
[外链图片转存中…(img-cqaeYtKT-1714122855290)]
当然在这还有更多整理总结的Java进阶学习笔记和面试题未展示,其中囊括了Dubbo、Redis、Netty、zookeeper、Spring cloud、分布式、高并发等架构资料和完整的Java架构学习进阶导图!
[外链图片转存中…(img-BC2IAEeM-1714122855291)]
更多Java架构进阶资料展示
[外链图片转存中…(img-HZE3ZRy8-1714122855291)]
[外链图片转存中…(img-Li8Yl5bS-1714122855292)]
[外链图片转存中…(img-1gQtcdiu-1714122855292)]