Java 8 Predicate示例
时间:2020-02-23 14:34:53 来源:igfitidea点击:
Java 8 Predicate是Java 8中引入的功能接口。
Predicate定义
Predicate
单个参数功能接口是否返回true或者false。
它需要一个参数并返回结果为true或者false。
//Using anonymous class Predicate<Integer> predicate=new Predicate<Integer>() { @Override public boolean test(Integer i) { return i > 100; } }; //Using lambda expression Predicate<Integer> predicate = i -> i > 100;
这是定义 Predicate
接口。
package java.util.function; import java.util.Objects; /** * Represents a predicate (boolean-valued function) of one argument. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #test(Object)}. * * @param <T> the type of the input to the predicate * * @since 1.8 */ @FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } default Predicate<T> negate() { return (t) -> !test(t); } default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }
Predicate方法示例
test()
这是 Predicate接口的抽象方法。
如果 Predicate与输入参数匹配,则会评估True。
这是一个简单的例子 test()
检查输入参数是否大于100或者不检查的方法。
package org.igi.theitroad; import java.util.function.Predicate; public class Java8PredicateExample { public static void main(String[] args) { Predicate<Integer> predicate = i -> i > 100; boolean greaterCheck = predicate.test(200); System.out.println("is 200 greater than 100: "+greaterCheck); } }
我们也可以将 Predicate作为函数参数传递。
package org.igi.theitroad; import java.util.function.Predicate; public class Java8PredicateMethodExample { public static void main(String[] args) { boolean greaterCheckBoolean = greaterCheck(200, p -> p > 100); System.out.println(greaterCheckBoolean); } public static boolean greaterCheck(int number, Predicate<Integer> predicate) { return predicate.test(number); } }
and()
and()是返回表示逻辑的复合 Predicate的默认方法,并通过 Predicate和传递的 Predicate。
package org.igi.theitroad; import java.util.function.Predicate; public class Java8PredicateAndExample { public static void main(String[] args) { Predicate<Integer> predicate1 = i -> i > 100; Predicate<Integer> predicate2 = i -> i < 300; Predicate<Integer> andPredicate = predicate1.and(predicate2); boolean rangeCheck = andPredicate.test(200); System.out.println("200 lies between 100 and 300: "+ rangeCheck); } }
我们已经创建了两个 Predicate并检查了逻辑和 Predicate1和 Predicate2
or()
or()是返回表示逻辑或者此 Predicate和传递 Predicate的复合 Predicate的默认方法。
package org.igi.theitroad; import java.util.function.Predicate; public class Java8PredicateAndExample { public static void main(String[] args) { Predicate<Integer> predicate1 = i -> i > 100; Predicate<Integer> predicate2 = i -> i < 50; Predicate<Integer> andPredicate = predicate1.or(predicate2); boolean rangeCheck = andPredicate.test(30); System.out.println("(30 > 100) or (30 < 50) returns: "+ rangeCheck); } }
我们已经创建了两个 Predicate并检查了逻辑或者 Predicate1和 Predicate2
negate()
negate()是返回 Predicate的默认方法,其表示逻辑否定此 Predicate。
package org.igi.theitroad; import java.util.function.Predicate; public class Java8PredicateAndExample { public static void main(String[] args) { Predicate<Integer> predicate = i -> i > 100; Predicate<Integer> NegatePredicate = predicate.negate(); //Negate predicate will become i -> i < 100 boolean rangeCheck = NegatePredicate.test(30); System.out.println("30 is less than 100: "+ rangeCheck); } }
isEqual()
isEqual()是静态方法返回 Predicate,该 Predicate是否基于对象的equals()方法等于两个参数。
package org.igi.theitroad; import java.util.function.Predicate; public class Java8PredicateAndExample { public static void main(String[] args) { Predicate<String> predicate = Predicate.isEqual("test1"); System.out.println(predicate.test("test1")); System.out.println(predicate.test("test2")); } }
使用 Predicate过滤列表
Java 8流 filter
方法需要 Predicate
作为一个参数,可以用来在 Predicate的帮助下过滤列表。
让我们说你有以下学生程序:
package org.igi.theitroad; public class Student { private int id; private String name; private String gender; private int age; public Student(int id, String name, String gender, int age) { super(); this.id = id; this.name = name; this.gender = gender; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]"; } }
允许创建一个函数,该函数将用于过滤基于 Predicate的学生。
public static List filterStudents (List students,Predicate predicate) { return students.stream().filter( predicate ).collect(Collectors.toList()); }
让我们创建一个主要类:
package org.igi.theitroad; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; public class Java8PredicateStudentExample { public static void main(String[] args) { List<Student> studentList = createStudentList(); //Filter all male student who have age > 18 Predicate<Student> predicate1 = s -> s.getGender().equalsIgnoreCase("M") && s.getAge() > 18; List<Student> students1 = filterStudents(studentList, predicate1); System.out.println("Male students having age > 18 are :" + students1); //Filer all female student who have age < 18 Predicate<Student> predicate2 = s -> s.getGender().equalsIgnoreCase("F") && s.getAge() < 18; List<Student> students2 = filterStudents(studentList, predicate2); System.out.println("Female students having age < 18 are :" + students2); } public static List<Student> filterStudents(List<Student> students, Predicate<Student> predicate) { return students.stream().filter(predicate).collect(Collectors.toList()); } public static List<Student> createStudentList() { List<Student> studentList = new ArrayList<>(); Student s1 = new Student(1, "igi", "M", 19); Student s2 = new Student(2, "John", "M", 17); Student s3 = new Student(3, "Mary", "F", 14); Student s4 = new Student(4, "Martin", "M", 21); Student s5 = new Student(5, "Monica", "F", 16); Student s6 = new Student(6, "Ally", "F", 20); studentList.add(s1); studentList.add(s2); studentList.add(s3); studentList.add(s4); studentList.add(s5); studentList.add(s6); return studentList; } }