前言
Java8是 Java 语言的一个重要版本,该版本于2014年3月发布,是自Java5以来最具革命性的版 本,这个版本包含语言、编译器、库、工具和JVM等方面的十多个新特性。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Predicate
Predicate
/*
* 判断一个参数是否符合要求
*/
@FunctionalInterface
public interface Predicate<T> {
// 用来处理参数T,判断是否符合条件
boolean test(T var1);
// 可理解为 条件A && 条件B
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> {
return this.test(t) && other.test(t);
};
}
// 对当前判断进行"!"操作,即取非操作,可理解为 ! 条件A
default Predicate<T> negate() {
return (t) -> {
return !this.test(t);
};
}
// 对当前判断进行"||"操作,即取或操作,可以理解为 条件A ||条件B
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> {
return this.test(t) || other.test(t);
};
}
// 对当前操作进行"="操作,即取等操作,可以理解为 A == B
static <T> Predicate<T> isEqual(Object targetRef) {
return null == targetRef ? Objects::isNull : (object) -> {
return targetRef.equals(object);
};
}
static <T> Predicate<T> not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return target.negate();
}
}
1. boolean test(T var1);
代码如下(示例):
Predicate<Integer> predicate = x -> x > 0;
System.out.println(predicate.test(10));//true
2. and(Predicate<? super T> other)
代码如下(示例):
Predicate<Integer> predicate = x -> x >100;
predicate = predicate.and(x -> x % 2 == 0 );
System.out.println(predicate.test(98));// false
System.out.println(predicate.test(102));// true
System.out.println(predicate.test(103));// false
3. negate()
代码如下(示例):
Predicate<Person> personPredicate = x -> x.age > 22;
System.out.println(
Stream.of(
new Person(21,"zhangsan"),
new Person(22,"lisi"),
new Person(23,"wangwu"),
new Person(24,"wangwu"),
new Person(25,"lisi"),
new Person(26,"zhangsan")
)
.filter(personPredicate.negate())
.count()
);// 4
4. or(Predicate<? super T> other)
代码如下(示例):
Predicate<Person> predicate = x -> x.name.equals("lisi");
predicate = predicate.or(x -> x.age > 25);
System.out.println(
Stream.of(
new Person(21,"zhangsan"),
new Person(22,"lisi"),
new Person(23,"wangwu"),
new Person(24,"wangwu"),
new Person(25,"lisi"),
new Person(26,"zhangsan")
)
.filter(predicate)
.count()
);
5. isEqual(Object targetRef)
代码如下(示例):
Person person = new Person(22,"lisi");
Predicate<Person> predicate = Predicate.isEqual(person);
System.out.println(
Stream.of(
new Person(21,"zhangsan"),
new Person(22,"lisi"),
new Person(23,"wangwu"),
new Person(24,"wangwu"),
new Person(22,"lisi"),
new Person(26,"zhangsan")
)
.filter(predicate)
.count()
);// 2
二、Consumer
Consumer 数据获取
@FunctionalInterface
public interface Consumer<T> {
void accept(T var1);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (t) -> {
this.accept(t);
after.accept(t);
};
}
}
1. accept(T var1)
代码如下(示例):
List<Person> lisiList = new ArrayList<>();
Consumer <Person> consumer = x -> {
if (x.name.equals("lisi")){
lisiList.add(x);
}
};
Stream.of(
new Person(21,"zhangsan"),
new Person(22,"lisi"),
new Person(23,"wangwu"),
new Person(24,"wangwu"),
new Person(23,"lisi"),
new Person(26,"lisi"),
new Person(26,"zhangsan")
).forEach(consumer);
System.out.println(JSON.toJSONString(lisiList));
2. andThen(Consumer<? super T> after)
代码如下(示例):
List<Person> lisiList = new ArrayList<>();
Consumer <Person> consumer = x -> {
if (x.name.equals("lisi")){
lisiList.add(x);
}
};
consumer = consumer.andThen(
x -> lisiList.removeIf(y -> y.age < 23)
);
Stream.of(
new Person(21,"zhangsan"),
new Person(22,"lisi"),
new Person(23,"wangwu"),
new Person(24,"wangwu"),
new Person(23,"lisi"),
new Person(26,"lisi"),
new Person(26,"zhangsan")
).forEach(consumer);
System.out.println(JSON.toJSONString(lisiList));
三、Function
Function 类型转换
@FunctionalInterface
public interface Function<T, R> {
R apply(T var1);
//将Function对象应用到输入的参数上,然后返回计算结果。
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (v) -> {
return this.apply(before.apply(v));
};
}
//返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (t) -> {
return after.apply(this.apply(t));
};
}
//返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象
static <T> Function<T, T> identity() {
return (t) -> {
return t;
};
}
}
1. apply(T var1)
代码如下(示例):
Function<Integer, Integer> name = e -> e * 2;
int value = name.apply(3);
System.out.println("andThen value=" + value);// 6
2. compose(Function<? super V, ? extends T> before)
代码如下(示例):
Function<Integer, Integer> name = e -> e * 2;
Function<Integer, Integer> square = e -> e * e;
// 运算逻辑 andThen 先计算前边的,再把得到的参数传入下一次计算
int value = name.andThen(square).apply(3);
System.out.println("andThen value=" + value);// 36
3. andThen(Function<? super R, ? extends V> after)
代码如下(示例):
Function<Integer, Integer> name = e -> e * 2;
Function<Integer, Integer> square = e -> e * e;
// compose 先计算后边的逻辑,再把参数传入前边的运算
int value2 = name.compose(square).apply(3);
System.out.println("compose value2=" + value2);// 18
4. identity()
代码如下(示例):
Function<Integer, Integer> name = e -> e * 2;
Function<Integer, Integer> square = e -> e * e;
Object identity = Function.identity().apply("huohuo");
System.out.println(identity);// huohuo
四、Supplier
Supplier 获取数值
1. get
@FunctionalInterface
public interface Supplier<T> {
T get();
}
代码如下(示例):
public static void main(String[] args) {
int[] numbers = {100, 200, 300, 400, 500, -600, -700, -800, -900, -1000};
int numberMax = arrayMax(
() -> {
int max = numbers[0];
for (int number : numbers) {
if (max < number) {
max = number;
}
}
return max;
}
);
System.out.println("数组中的最大值为:" + numberMax);
}
public static Integer arrayMax(Supplier<Integer> s){
return s.get();
}