Table of Contents
TogglePredicate in Java with Examples
只有一個抽象方法的接口,定義了 test 方法,可以在一個元素上測試或應用一些操作,常用於過濾條件,如:過濾數量介於 3 – 6 之間,取代傳統實作接口的方法,讓程式碼更加簡潔和易讀, Predicate in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- predicate
| +- PredicateWithExamplesTest.java
單元測試
Predicate Java 提供 test 、 and 、 or 條件或組合成鏈式判斷等操作 Predicate 。
Food
建立 Food 類別,覆寫 equals 、 hashCode ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Food {
private String name;
private double quantity;
private int type;
public Food(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Food)) {
return false;
}
if (this == object) {
return true;
}
Food other = (Food) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
test
建立 Predicate 物件,傳入 1 個參數,判斷是否為真。
@Test
public void test() {
Predicate<Food> lessThan = o -> o.quantity < 3;
Food food = new Food("Bacon", 1, 1);
boolean result = lessThan.test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Pork", 3, 1);
result = lessThan.test(food);
System.out.println(result);
assertFalse(result);
}
true
false
negate
Predicate Java 建立 Predicate 物件,傳入 1 個參數,否定判斷。
@Test
public void negate() {
Predicate<Food> lessThan = o -> o.quantity < 3;
Food food = new Food("Bacon", 1, 1);
boolean result = lessThan.negate().test(food);
System.out.println(result);
assertFalse(result);
food = new Food("Pork", 3, 1);
result = lessThan.negate().test(food);
System.out.println(result);
assertTrue(result);
}
false
true
and
Predicate Java 建立 2 個 Predicate 物件,各傳入 1 個參數,使用 and 組合判斷是否為真,並傳回 Predicate 是一個短路邏輯運算,其中當第一個 Predicate 為 False 時將不會評估第二個 Predicate 。
@Test
public void and() {
Predicate<Food> nonNull = Objects::nonNull;
Predicate<Food> contains = o -> o.name.contains("o");
Food food = new Food("Bacon", 1, 1);
boolean result = nonNull.and(contains).test(food);
System.out.println(result);
assertTrue(result);
result = nonNull.and(contains).test(null);
System.out.println(result);
assertFalse(result);
}
true
false
or
Predicate Java 建立 2 個 Predicate 物件,各傳入 1 個參數,使用 or 組合判斷是否為真,並傳回 Predicate 是一個短路邏輯運算,其中當第一個 Predicate 為 True 時將不會評估第二個 Predicate 。
@Test
public void or() {
Predicate<Food> isNull = Objects::isNull;
Predicate<Food> contains = o -> o.name.contains("o");
Food food = new Food("Pork", 3, 1);
boolean result = isNull.or(contains).test(food);
System.out.println(result);
assertTrue(result);
result = isNull.or(contains).test(null);
System.out.println(result);
assertTrue(result);
}
true
true
chaining
Java Predicate 建立多個 Predicate 物件,各傳入 1 個參數,使用 and 、 or 組合判斷是否為真。
@Test
public void chaining() {
Predicate<Food> nonNull = Objects::nonNull;
Predicate<Food> startsWith = o -> o.name.startsWith("B");
Predicate<Food> endsWith = o -> o.name.endsWith("n");
Food food = new Food("Bacon", 1, 1);
boolean result = nonNull.and(startsWith).or(endsWith).test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = nonNull.and(startsWith).or(endsWith).test(food);
System.out.println(result);
assertFalse(result);
}
true
false
isEqual
Java Predicate 建立 Predicate 物件,傳入 1 個參數,比較是否相等。
@Test
public void isEqual() {
Food food = new Food("Bacon", 1, 1);
Predicate<Food> isEqual = Predicate.isEqual(food);
boolean result = isEqual.test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = isEqual.test(food);
System.out.println(result);
assertFalse(result);
}
true
false
traditional
Java Predicate 使用傳統方式,實作 Predicate 接口,判斷是否為真。
public static class LengthGreaterThan<E> implements Predicate<Food> {
@Override
public boolean test(Food t) {
return t.name.length() > 3;
}
}
@Test
public void traditional() {
Predicate<Food> lengthGreaterThan = new LengthGreaterThan<Food>();
Predicate<Food> contains = o -> o.name.contains("o");
Food food = new Food("Bacon", 1, 1);
boolean result = lengthGreaterThan.and(contains).test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = lengthGreaterThan.and(contains).test(food);
System.out.println(result);
assertFalse(result);
}
true
false
PredicateWithExamplesTest.java
Predicate in Java 新增單元測試,驗證 Java Predicate Example 是否符合預期。
package org.ruoxue.java_147.functional.predicate;
import static org.junit.Assert.*;
import java.util.function.Predicate;
import java.util.Objects;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class PredicateWithExamplesTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Food {
private String name;
private double quantity;
private int type;
public Food(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Food)) {
return false;
}
if (this == object) {
return true;
}
Food other = (Food) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
@Test
public void test() {
Predicate<Food> lessThan = o -> o.quantity < 3;
Food food = new Food("Bacon", 1, 1);
boolean result = lessThan.test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Pork", 3, 1);
result = lessThan.test(food);
System.out.println(result);
assertFalse(result);
}
@Test
public void negate() {
Predicate<Food> lessThan = o -> o.quantity < 3;
Food food = new Food("Bacon", 1, 1);
boolean result = lessThan.negate().test(food);
System.out.println(result);
assertFalse(result);
food = new Food("Pork", 3, 1);
result = lessThan.negate().test(food);
System.out.println(result);
assertTrue(result);
}
@Test
public void and() {
Predicate<Food> nonNull = Objects::nonNull;
Predicate<Food> contains = o -> o.name.contains("o");
Food food = new Food("Bacon", 1, 1);
boolean result = nonNull.and(contains).test(food);
System.out.println(result);
assertTrue(result);
result = nonNull.and(contains).test(null);
System.out.println(result);
assertFalse(result);
}
@Test
public void or() {
Predicate<Food> isNull = Objects::isNull;
Predicate<Food> contains = o -> o.name.contains("o");
Food food = new Food("Pork", 3, 1);
boolean result = isNull.or(contains).test(food);
System.out.println(result);
assertTrue(result);
result = isNull.or(contains).test(null);
System.out.println(result);
assertTrue(result);
}
@Test
public void chaining() {
Predicate<Food> nonNull = Objects::nonNull;
Predicate<Food> startsWith = o -> o.name.startsWith("B");
Predicate<Food> endsWith = o -> o.name.endsWith("n");
Food food = new Food("Bacon", 1, 1);
boolean result = nonNull.and(startsWith).or(endsWith).test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = nonNull.and(startsWith).or(endsWith).test(food);
System.out.println(result);
assertFalse(result);
}
@Test
public void isEqual() {
Food food = new Food("Bacon", 1, 1);
Predicate<Food> isEqual = Predicate.isEqual(food);
boolean result = isEqual.test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = isEqual.test(food);
System.out.println(result);
assertFalse(result);
}
public static class LengthGreaterThan<E> implements Predicate<Food> {
@Override
public boolean test(Food t) {
return t.name.length() > 3;
}
}
@Test
public void traditional() {
Predicate<Food> lengthGreaterThan = new LengthGreaterThan<Food>();
Predicate<Food> contains = o -> o.name.contains("o");
Food food = new Food("Bacon", 1, 1);
boolean result = lengthGreaterThan.and(contains).test(food);
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = lengthGreaterThan.and(contains).test(food);
System.out.println(result);
assertFalse(result);
}
}
心得分享
Java Predicate Example 功能接口,使用 Lambda 語法,可當作其他方法的傳入參數或是引用其他方法為實例, Java Predicate 提供了幾種 Predicate 常見方法的操作範例,例如: test 、 and 、 or 等方法。