Java-8-Predicate
Interface Predicate<T>
Predicate函数式接口的主要作用就是提供一个test方法,接受一个参数返回一个布尔类型
接口源码:
@FunctionalInterface
public interface Predicate<T> {
/**
* 具体过滤操作 需要被子类实现.
* 用来处理参数T是否满足要求,可以理解为 条件A
*/
boolean test(T t);
/**
* 调用当前Predicate的test方法之后再去调用other的test方法,相当于进行两次判断
* 可理解为 条件A && 条件B
*/
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
/**
* 对当前判断进行"!"操作,即取非操作,可理解为 ! 条件A
*/
default Predicate<T> negate() {
return (t) -> !test(t);
}
/**
* 对当前判断进行"||"操作,即取或操作,可以理解为 条件A ||条件B
*/
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
/**
* 对当前操作进行"="操作,即取等操作,可以理解为 A == B
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
简单例子
public class M1 {
public static void main(String[] args) {
// 判断整型是否大于50
Predicate<Integer> p1 = integer -> integer > 50;
// Random random = new Random(55);
int[] nums = new int[]
{
11,58,6345,5444,0,12,355
};
for (int i : nums){
System.out.println(i + " 是否大于50 " + p1.test(i));
}
System.out.println("--------------");
Predicate<String> p2 = s -> s.length()>5;
String[] strings = new String[]
{"jdjfjfjhhu","kkjk","djkjdj","4568","hello"};
for (String s : strings){
System.out.println(s + " 长度是否大于5 " + p2.test(s));
}
System.out.println("--------------");
Predicate<String> p3 = s -> s.length() > 3;
Predicate<String> p4 = s -> s.startsWith("A");
String[] strings1 = new String[]
{"Ajdd","2121aA","ajdjdj","af","AAdkdf"};
for (String s : strings1){
if (p3.and(p4).test(s)){
System.out.println(s);
}
}
System.out.println("--------------");
Predicate<Double> p5 = d -> d > 100;
double[] doubles = new double[]
{112.2,54,154,25.36,0,232,25.3};
for (Double d : doubles) {
if (p5.negate().test(d)){
System.out.println(d);
}
}
System.out.println("--------------");
Predicate<String> p6 = s -> s.length() < 5;
Predicate<String> p7 = s -> s.endsWith("C");
String[] strings2 = new String[]
{"adhaC","aaadefgrgr","d"};
for (String s : strings2){
if (p6.or(p7).test(s)){
System.out.println(s);
}
}
System.out.println("--------------");
Predicate<String> p8 = Predicate.isEqual("abc");
System.out.println(p8.test("abc"));
System.out.println(p8.test("abcasasas"));
}
}
把条件进行封装
public class M2 {
public static void main(String[] args) {
Predicate<Integer> predicate1 = integer -> integer < 15;
System.out.println(
conditionFilter(Create_Data.supply_Integers(),predicate1)
);
}
//高度抽象的方法定义,复用性高
public static <T> List<T> conditionFilter(List<T> list,
Predicate<T> predicate){
return list.stream()
.filter(predicate)
.collect(Collectors.toList());
}
}
针对特定类型
DoublePredicate LongPredicate IntPredicate
public class M1 {
public static void main(String[] args) {
DoublePredicate doubleDoublePredicate = d -> d > 500;
double[] doubles = {15.5,556.5,225,44585,2565.336};
for (int i = 0; i < doubles.length; i++) {
if (doubleDoublePredicate.test(doubles[i])){
System.out.println(doubles[i]);
}
}
System.out.println("-----------------------");
IntPredicate intPredicate = i -> i < 100;
int[] ints = {11,125,55,889,478};
for (int i = 0; i < ints.length; i++) {
if (intPredicate.test(ints[i])){
System.out.println(ints[i]);
}
}
System.out.println("-----------------------");
LongPredicate longPredicate = l -> l < 5;
long[] longs = {1L,15L,12L,6L,77L};
for (int i = 0; i < longs.length; i++) {
if (longPredicate.test(longs[i])){
System.out.println(longs[i]);
}
}
}
}
BiPredicate<T, U>
前面的都是接受一个参数,而BiPredicate<T, U>可以对给定的两个输入参数执行操作,返回一个boolean类型的结果(布尔值函数)
结合具体对象进行操作
public class Person {
private String name;
private int age;
private String origin;
public Person(String name, int age, String origin) {
this.name = name;
this.age = age;
this.origin = origin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", origin='" + origin + '\'' +
'}';
}
}
public class M2 {
public static void main(String[] args) {
// 找出 来自上海 并且 年纪在30岁以上的人
BiPredicate<Integer,String> biPredicate = (i,s)->
i > 30 && s.equals("Shanghai");
List<Person> personList = Create_Data.supply_Persons();
for (Person person : personList){
if (biPredicate.test(person.getAge(),person.getOrigin())){
System.out.println(person);
}
}
}
}
实现自己的Predicate
@FunctionalInterface
public interface MyPredicate<T,R,U> {
boolean test(T t,R r,U u);
}
public class Student {
private String name;
private int age;
private float height;
public Student(String name, int age, float height) {
this.name = name;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
public class M1 {
public static void main(String[] args) {
MyPredicate<Integer,String,Float> myPredicate = (i,s,f) ->
i > 12 && s.length()>2 && f>0.1;
List<Student> students = Create_Data.supply_Student();
for (Student student : students) {
if (myPredicate.test(student.getAge(),student.getName(),student.getHeight())){
System.out.println(student);
}
}
}
}
网友评论