- 应用场景介绍,两个集合对象List 之间求取差集,根据每一个字段判断。
Java 8 Stream allMatch, anyMatch和noneMatch方法应用于与给定Predicate匹配的流对象,然后返回布尔值。
allMatch()检查调用流是否与给定Predicate完全匹配,如果是,则返回true,否则返回false。
anyMatch()检查流中是否有与给定Predicate匹配的元素,如果是,则返回true,否则返回false。
noneMatch()仅当没有元素与给定Predicate匹配才返回true。
Stream.allMatch
我们将Predicate作为参数传递给allMatch()方法。
该Predicate应用于流的每个元素,如果每个元素都满足给定的Predicate,则返回true,否则返回false。
Stream.anyMatch
对于anyMatch()方法,我们将Predicate作为参数传递。
流的元素为此Predicate进行迭代。如果有元素匹配,则返回true,否则返回false。
Stream.noneMatch
对于noneMatch()方法,我们将Predicate作为参数传递。
如果流中的任何元素都不匹配给定的Predicate,则返回true,否则返回false。
- 准备使用的类直接上代码,先创建两个类,当然内部类也可以。
public class ExecutorTest3 {
class ClassA{
String id;
String name;
public ClassA(String id, String name) {
this.id = id;
this.name = name;
}
public ClassA() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class ClassB{
String id;
String name;
public ClassB(String id, String name) {
this.id = id;
this.name = name;
}
public ClassB() {