原文鏈接 http://ifeve.com/predicate-and-consumer-interface-in-java-util-function-package-in-java-8/
原文鏈接 作者: Mohamed Sanaulla 譯者: 李璟(jlee381344197@gmail.com)
早先我寫了一篇《函數式接口》,探討了部分Java 8中函數式接口的用法。我也提及了Predicate接口屬於java.util.function包,
在這篇文章中,我將展示如何應用Predicate接口和Consumer接口。
一起看一下Predicate的官方文檔:
Determines if the input object matches some criteria.
即判斷輸入的對象是否符合某個條件。
在Predicate接口中,有以下5個方法(你肯定好奇為何此接口屬於函數式接口。如果你這么想,在使用接口前應該好好研讀方法的注釋):
03 |
and(Predicate<? super T> p) |
10 |
or(Predicate<? super T> p) |
17 |
xor(Predicate<? super T> p) |
除了test()方法是抽象方法以外,其他方法都是默認方法(譯者注:在Java 8中,接口可以包含帶有實現代碼的方法,這些方法稱為default方法)。
可以使用匿名內部類提供test()方法的實現,也可以使用lambda表達式實現test()。
Consumer接口的文檔聲明如下:
An operation which accepts a single input argument and returns no result. Unlike most other functional interfaces,
Consumer is expected to operate via side-effects.
即接口表示一個接受單個輸入參數並且沒有返回值的操作。不像其他函數式接口,Consumer接口期望執行帶有副作用的操作
(譯者注:Consumer的操作可能會更改輸入參數的內部狀態)。
Consumer接口中有2個方法,有且只有一個聲明為accept(T t)的方法,接收一個輸入參數並且沒有返回值。
為了詳細說明Predicate和Consumer接口,我們來考慮一下學生的例子:Student類包含姓名,分數以及待付費用,
每個學生可根據分數獲得不同程度的費用折扣。
09 |
Double feeDiscount = 0.0 ; |
11 |
Double baseFee = 20000.0 ; |
13 |
public Student(String firstName, String lastName, Double grade) { |
15 |
this .firstName = firstName; |
17 |
this .lastName = lastName; |
22 |
public void printFee(){ |
24 |
Double newFee = baseFee - ((baseFee * feeDiscount) / 100 ); |
26 |
System.out.println( "The fee after discount: " + newFee); |
我們分別聲明一個接受Student對象的Predicate接口以及Consumer接口的實現類。如果你還不熟悉Function接口,
那么你需要花幾分鍾閱讀一下這篇文章。這個例子使用Predicate接口實現類的test()方法判斷輸入的Student對象是否擁有費用打折的資格,
然后使用Consumer接口的實現類更新輸入的Student對象的折扣。
01 |
public class PreidcateConsumerDemo { |
03 |
public static Student updateStudentFee(Student student, Predicate<Student> predicate, Consumer<Student> consumer){ |
07 |
if ( predicate.test(student)){ |
11 |
consumer.accept(student); |
Predicate和Consumer接口的test()和accept()方法都接受一個泛型參數。不同的是test()方法進行某些邏輯判斷並返回一個boolean值,
而accept()接受並改變某個對象的內部值。updateStudentFee方法的調用如下所示:
01 |
public static void main(String[] args) { |
03 |
Student student1 = new Student( "Ashok" , "Kumar" , 9.5 ); |
05 |
student1 = updateStudentFee(student1, |
07 |
student -> student.grade > 8.5 , |
09 |
student -> student.feeDiscount = 30.0 ); |
13 |
Student student2 = new Student( "Rajat" , "Verma" , 8.0 ); |
15 |
student2 = updateStudentFee(student2, |
16 |
student -> student.grade >= 8 , |
17 |
student -> student.feeDiscount = 20.0 ); |
李 璟目前就職於深圳金斧子網絡科技,關注Java、Python服務端開發
1 package java.util.function;
2
3 import java.util.Objects;
4
5 /**
6 * Represents a predicate (boolean-valued function) of one argument.
7 *
8 * <p>This is a <a href="package-summary.html">functional interface</a>
9 * whose functional method is {@link #test(Object)}.
10 *
11 * @param <T> the type of the input to the predicate
12 *
13 * @since 1.8
14 */
15 @FunctionalInterface
16 public interface Predicate<T> {
17
18 /** 19 * Evaluates this predicate on the given argument. 20 * 21 * @param t the input argument 22 * @return {@code true} if the input argument matches the predicate, 23 * otherwise {@code false} 24 */ 25 boolean test(T t); 26
27 /**
28 * Returns a composed predicate that represents a short-circuiting logical
29 * AND of this predicate and another. When evaluating the composed
30 * predicate, if this predicate is {@code false}, then the {@code other}
31 * predicate is not evaluated.
32 *
33 * <p>Any exceptions thrown during evaluation of either predicate are relayed
34 * to the caller; if evaluation of this predicate throws an exception, the
35 * {@code other} predicate will not be evaluated.
36 *
37 * @param other a predicate that will be logically-ANDed with this
38 * predicate
39 * @return a composed predicate that represents the short-circuiting logical
40 * AND of this predicate and the {@code other} predicate
41 * @throws NullPointerException if other is null
42 */
43 default Predicate<T> and(Predicate<? super T> other) {
44 Objects.requireNonNull(other);
45 return (t) -> test(t) && other.test(t);
46 }
47
48 /**
49 * Returns a predicate that represents the logical negation of this
50 * predicate.
51 *
52 * @return a predicate that represents the logical negation of this
53 * predicate
54 */
55 default Predicate<T> negate() {
56 return (t) -> !test(t);
57 }
58
59 /**
60 * Returns a composed predicate that represents a short-circuiting logical
61 * OR of this predicate and another. When evaluating the composed
62 * predicate, if this predicate is {@code true}, then the {@code other}
63 * predicate is not evaluated.
64 *
65 * <p>Any exceptions thrown during evaluation of either predicate are relayed
66 * to the caller; if evaluation of this predicate throws an exception, the
67 * {@code other} predicate will not be evaluated.
68 *
69 * @param other a predicate that will be logically-ORed with this
70 * predicate
71 * @return a composed predicate that represents the short-circuiting logical
72 * OR of this predicate and the {@code other} predicate
73 * @throws NullPointerException if other is null
74 */
75 default Predicate<T> or(Predicate<? super T> other) {
76 Objects.requireNonNull(other);
77 return (t) -> test(t) || other.test(t);
78 }
79
80 /**
81 * Returns a predicate that tests if two arguments are equal according
82 * to {@link Objects#equals(Object, Object)}.
83 *
84 * @param <T> the type of arguments to the predicate
85 * @param targetRef the object reference with which to compare for equality,
86 * which may be {@code null}
87 * @return a predicate that tests if two arguments are equal according
88 * to {@link Objects#equals(Object, Object)}
89 */
90 static <T> Predicate<T> isEqual(Object targetRef) {
91 return (null == targetRef)
92 ? Objects::isNull
93 : object -> targetRef.equals(object);
94 }
95 }