Code:
public interface INumberFunction{
void change();
void delete();
int getNumber();
default void currentNumber(){
System.out.println(“current”);
callPrivateMethod();
}
private void callPrivateMethod(){
System.out.println(“jdk9 level const method”);
}
}
<APIS 1.8+>
Predicate:
源码:
//
// 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 Predicate {
boolean test(T var1);
default Predicate and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> {
return this.test(t) && other.test(t);
};
}
default Predicate negate() {
return (t) -> {
return !this.test(t);
};
}
default Predicate or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> {
return this.test(t) || other.test(t);
};
}
static Predicate isEqual(Object targetRef) {
return null == targetRef ? Objects::isNull : (object) -> {
return targetRef.equals(object);
};
}
static Predicate not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return target.negate();
}
}
可以理解为一个比较器 常用方法 test() 返回一个boolean 值
negate() 取反比较(相比于test) 返回当前实例 可以链式调用
![](https://img-blog.csdnimg.cn/20200529140946552.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5NzY5ODUx,size_16,color_FFFFFF,t_70)
执行结果:
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方法,可以理解为是一个抽象工厂模式,下面我们进行测试一下.