Table of Contents
ToggleBiPredicate in Java with Examples
只有一個抽象方法的接口,定義了 test 方法,可以在一個元素上測試或應用一些操作,常用於過濾條件,如:過濾數量介於 3 – 6 之間,取代傳統實作接口的方法,讓程式碼更加簡潔和易讀, BiPredicate in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface BiPredicate<T> {
boolean test(T t, U u);
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- bipredicate
| +- BiPredicateWithExamplesTest.java
單元測試
BiPredicate Java 提供 test 、 and 、 or 條件或組合成鏈式判斷等操作 BiPredicate 。
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
建立 BiPredicate 物件,傳入 2 個參數,判斷是否為真。
@Test
public void test() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 3, 1);
result = startsWith.test(food, "B");
System.out.println(result);
assertFalse(result);
}
true
false
negate
BiPredicate Java 建立 BiPredicate 物件,傳入 2 個參數,否定判斷。
@Test
public void negate() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.negate().test(food, "B");
System.out.println(result);
assertFalse(result);
food = new Food("Ham", 3, 1);
result = startsWith.negate().test(food, "B");
System.out.println(result);
assertTrue(result);
}
false
true
and
BiPredicate Java 建立 2 個 BiPredicate 物件,各傳入 2個參數,使用 and 組合判斷是否為真,並傳回 BiPredicate 是一個短路邏輯運算,其中當第一個 BiPredicate 為 False 時將不會評估第二個 BiPredicate 。
@Test
public void and() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 3, 1);
result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertFalse(result);
}
true
false
or
BiPredicate Java 建立 2 個 BiPredicate 物件,各傳入 2 個參數,使用 or 組合判斷是否為真,並傳回 BiPredicate 是一個短路邏輯運算,其中當第一個 BiPredicate 為 True 時將不會評估第二個 BiPredicate 。
@Test
public void or() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
Food food = new Food("Pork", 3, 1);
boolean result = startsWith.or(contains).test(food, "o");
System.out.println(result);
assertTrue(result);
result = startsWith.or(contains).test(food, "k");
System.out.println(result);
assertTrue(result);
}
true
true
chaining
Java BiPredicate 建立多個 BiPredicate 物件,各傳入 2 個參數,使用 and 、 or 組合判斷是否為真。
@Test
public void chaining() {
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
BiPredicate<Food, String> endsWith = (o, s) -> o.name.endsWith(s);
Food food = new Food("BaconB", 1, 1);
boolean result = contains.and(startsWith).or(endsWith).test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = contains.and(startsWith).or(endsWith).test(food, "B");
System.out.println(result);
assertFalse(result);
}
true
false
traditional
Java BiPredicate 使用傳統方式,實作 BiPredicate 接口,判斷是否為真。
public static class StartsWith<E> implements BiPredicate<Food, String> {
@Override
public boolean test(Food t, String u) {
return t.name.startsWith(u);
}
}
@Test
public void traditional() {
BiPredicate<Food, String> startsWith = new StartsWith<Food>();
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertFalse(result);
}
true
false
BiPredicateWithExamplesTest.java
BiPredicate in Java 新增單元測試,驗證 Java BiPredicate Example 是否符合預期。
package org.ruoxue.java_147.functional.bipredicate;
import static org.junit.Assert.*;
import java.util.function.BiPredicate;
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 BiPredicateWithExamplesTest {
@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() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 3, 1);
result = startsWith.test(food, "B");
System.out.println(result);
assertFalse(result);
}
@Test
public void negate() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.negate().test(food, "B");
System.out.println(result);
assertFalse(result);
food = new Food("Ham", 3, 1);
result = startsWith.negate().test(food, "B");
System.out.println(result);
assertTrue(result);
}
@Test
public void and() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 3, 1);
result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertFalse(result);
}
@Test
public void or() {
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
Food food = new Food("Pork", 3, 1);
boolean result = startsWith.or(contains).test(food, "o");
System.out.println(result);
assertTrue(result);
result = startsWith.or(contains).test(food, "k");
System.out.println(result);
assertTrue(result);
}
@Test
public void chaining() {
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
BiPredicate<Food, String> startsWith = (o, s) -> o.name.startsWith(s);
BiPredicate<Food, String> endsWith = (o, s) -> o.name.endsWith(s);
Food food = new Food("BaconB", 1, 1);
boolean result = contains.and(startsWith).or(endsWith).test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = contains.and(startsWith).or(endsWith).test(food, "B");
System.out.println(result);
assertFalse(result);
}
public static class StartsWith<E> implements BiPredicate<Food, String> {
@Override
public boolean test(Food t, String u) {
return t.name.startsWith(u);
}
}
@Test
public void traditional() {
BiPredicate<Food, String> startsWith = new StartsWith<Food>();
BiPredicate<Food, String> contains = (o, s) -> o.name.contains(s);
Food food = new Food("Bacon", 1, 1);
boolean result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertTrue(result);
food = new Food("Ham", 2, 1);
result = startsWith.and(contains).test(food, "B");
System.out.println(result);
assertFalse(result);
}
}
心得分享
Java BiPredicate Example 功能接口,使用 Lambda 語法,可當作其他方法的傳入參數或是引用其他方法為實例, Java BiPredicate 提供了幾種 BiPredicate 常見方法的操作範例,例如: test 、 and 、 or 等方法。