Table of Contents
ToggleBiConsumer in Java with Examples
只有一個抽象方法的接口,定義了 accept 方法,可以在一個元素上測試或應用一些操作,常用於判斷是否存在,才會執行程式邏輯,如: Optional ifPresent 等方法,取代傳統實作接口的方法,讓程式碼更加簡潔和易讀, BiConsumer in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface BiConsumer<T, U> {
void accept(T t, U u);
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- biconsumer
| +- BiConsumerWithExamplesTest.java
單元測試
BiConsumer Java 提供 accept 、 andThen 條件或組合成鏈式判斷等操作 BiConsumer 。
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();
}
}
accept
BiConsumer Java 建立 BiConsumer 物件,傳入 2 個參數,執行程式邏輯。
@Test
public void accept() {
BiConsumer<Food, Integer> lessThan = (o, i) -> System.out.println(o.quantity < i);
Food food = new Food("Bacon", 1, 1);
lessThan.accept(food, 3);
food = new Food("Pork", 3, 1);
lessThan.accept(food, 3);
}
true
false
andThen
BiConsumer Java 建立 2 個 BiConsumer 物件,各傳入 2個參數,使用 andThen 組合執行程式邏輯,並傳回 BiConsumer 繼續往下執行,其中若有例外拋出,將會中斷執行。
@Test
public void andThen() {
BiConsumer<Food, String> startsWith = (o, s) -> {
Optional<Food> opt = Optional.ofNullable(o);
opt.ifPresent(e -> System.out.println(e.name.startsWith(s)));
};
BiConsumer<Food, String> contains = (o, s) -> {
Optional<Food> opt = Optional.ofNullable(o);
opt.ifPresent(e -> System.out.println(e.name.contains(s)));
};
Food food = new Food("Bacon", 1, 1);
startsWith.andThen(contains).accept(food, "B");
startsWith.andThen(contains).accept(null, "B");
}
true
true
chaining
Java BiConsumer 建立多個 BiConsumer 物件,各傳入 2 個參數,使用 andThen 組合執行程式邏輯。
@Test
public void chaining() {
BiConsumer<Food, String> contains = (o, s) -> System.out.println(o.name.contains(s));
BiConsumer<Food, String> startsWith = (o, s) -> System.out.println(o.name.startsWith(s));
BiConsumer<Food, String> endsWith = (o, s) -> System.out.println(o.name.endsWith(s));
Food food = new Food("Bacon", 1, 1);
contains.andThen(startsWith).andThen(endsWith).accept(food, "B");
food = new Food("Ham", 2, 1);
contains.andThen(startsWith).andThen(endsWith).accept(food, "B");
}
true
true
false
false
false
false
traditional
Java BiConsumer 使用傳統方式,實作 BiConsumer 接口,判斷是否為真。
public static class LengthGreaterThan<E> implements BiConsumer<Food, Integer> {
@Override
public void accept(Food t, Integer u) {
System.out.println(t.name.length() > u);
}
}
@Test
public void traditional() {
BiConsumer<Food, Integer> lengthGreaterThan = new LengthGreaterThan<Food>();
BiConsumer<Food, Integer> lengthMod = (o, i) -> System.out.println(o.name.length() % i == 1);
Food food = new Food("Bacon", 1, 1);
lengthGreaterThan.andThen(lengthMod).accept(food, 4);
food = new Food("Ham", 2, 1);
lengthGreaterThan.andThen(lengthMod).accept(food, 3);
}
true
true
false
false
BiConsumerWithExamplesTest.java
BiConsumer in Java 新增單元測試,驗證 Java BiConsumer Example 是否符合預期。
package org.ruoxue.java_147.functional.biconsumer;
import java.util.function.BiConsumer;
import java.util.Optional;
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 BiConsumerWithExamplesTest {
@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 accept() {
BiConsumer<Food, Integer> lessThan = (o, i) -> System.out.println(o.quantity < i);
Food food = new Food("Bacon", 1, 1);
lessThan.accept(food, 3);
food = new Food("Pork", 3, 1);
lessThan.accept(food, 3);
}
@Test
public void andThen() {
BiConsumer<Food, String> startsWith = (o, s) -> {
Optional<Food> opt = Optional.ofNullable(o);
opt.ifPresent(e -> System.out.println(e.name.startsWith(s)));
};
BiConsumer<Food, String> contains = (o, s) -> {
Optional<Food> opt = Optional.ofNullable(o);
opt.ifPresent(e -> System.out.println(e.name.contains(s)));
};
Food food = new Food("Bacon", 1, 1);
startsWith.andThen(contains).accept(food, "B");
startsWith.andThen(contains).accept(null, "B");
}
@Test
public void chaining() {
BiConsumer<Food, String> contains = (o, s) -> System.out.println(o.name.contains(s));
BiConsumer<Food, String> startsWith = (o, s) -> System.out.println(o.name.startsWith(s));
BiConsumer<Food, String> endsWith = (o, s) -> System.out.println(o.name.endsWith(s));
Food food = new Food("Bacon", 1, 1);
contains.andThen(startsWith).andThen(endsWith).accept(food, "B");
food = new Food("Ham", 2, 1);
contains.andThen(startsWith).andThen(endsWith).accept(food, "B");
}
public static class LengthGreaterThan<E> implements BiConsumer<Food, Integer> {
@Override
public void accept(Food t, Integer u) {
System.out.println(t.name.length() > u);
}
}
@Test
public void traditional() {
BiConsumer<Food, Integer> lengthGreaterThan = new LengthGreaterThan<Food>();
BiConsumer<Food, Integer> lengthMod = (o, i) -> System.out.println(o.name.length() % i == 1);
Food food = new Food("Bacon", 1, 1);
lengthGreaterThan.andThen(lengthMod).accept(food, 4);
food = new Food("Ham", 2, 1);
lengthGreaterThan.andThen(lengthMod).accept(food, 3);
}
}
心得分享
Java BiConsumer Example 功能接口,使用 Lambda 語法,可當作其他方法的傳入參數或是引用其他方法為實例, Java BiConsumer 提供了幾種 BiConsumer 常見方法的操作範例,例如: accept 、 andThen 等方法。