1.源码:
@FunctionalInterface public interface Predicate{ boolean test(T t); default Predicate and(Predicate super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } default Predicate negate() { return (t) -> !test(t); } default Predicate or(Predicate super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } static Predicate isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }
简单案例:
public static void main(String[] args) {
//设置一个大于7的过滤条件
Predicate predicate = x -> x > 7;
System.out.println(predicate.test(10)); //输出 true
System.out.println(predicate.test(6)); //输出 fasle
//在上面大于7的条件下,添加是偶数的条件
predicate = predicate.and(x -> x % 2 == 0);
System.out.println(predicate.test(6)); //输出 fasle
System.out.println(predicate.test(12)); //输出 true
System.out.println(predicate.test(13)); //输出 fasle
predicate = x -> x > 5 && x < 9;
System.out.println(predicate.test(10)); //输出 false
System.out.println(predicate.test(6)); //输出 true
}
案例代码:
实体:
package com.study.stream; import java.util.Objects; public class Person implements Comparable{ private String name; private Integer age; private Integer height; public Person(String name, Integer age, Integer height) { this.name = name; this.age = age; this.height = height; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return name.equals(person.name) && age.equals(person.age); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return String.format("姓名:%s;年龄:%s;身高:%s;",this.name,this.age,this.height); } @Override public int compareTo(Person o) { return this.getAge().compareTo(o.getAge()); } }
案例:
package com.study.stream;
import java.util.function.Predicate;
public class PredicateDomel {
public static void main(String[] agr){
Predicate predicate= x -> x>2;
System.out.println("***********-- predicate test 判断是否符合--************");
System.out.println(predicate.test(1));
Predicate predicate1= x -> x<10;
System.out.println("***********-- predicate and 两个--************");
System.out.println(predicate.and(predicate1).test(6));
System.out.println(predicate.and(predicate1).test(40));
System.out.println("***********-- predicate or 或--************");
System.out.println(predicate.or(predicate1).test(40));
System.out.println("***********-- predicate negate 相反--************");
System.out.println(predicate.negate().test(4));
System.out.println("***********-- predicate isEqual 比较相等--************");
Person person= new Person("张一",18,172);
Person person1= new Person("张一",18,175);
System.out.println(Predicate.isEqual(person).test(person1));
}
}



