Table of Contents
ToggleBiFunction Functional Interface in Java
可當作其他方法的傳入參數或是引用其他方法為實例,使用 Lambda 語法,傳入 2 個泛型物件參數,結果傳回泛型物件,可以使用 andThen 組合成鏈式判斷, Functional Interface BiFunction 介紹常見的方法引用、方法參數等操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface BiFunction<T> {
boolean test(T t, U u);
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- bifunction
| +- BiFunctionFunctionalTest.java
單元測試
BiFunction Functional Interface 提供方法引用、參考等操作 Java BiFunction Functional Interface 。
Food
Function Functional Interface 建立 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();
}
}
methodReference
BiFunction Functional Interface 建立一個 BiFunction ,引用其他方法為實例,執行程式邏輯,傳回物件。
@Test
public void methodReference() {
BiFunction<String, String, Locale> locale = Locale::new;
Locale result = locale.apply("zh", "TW");
System.out.println(result);
assertNotNull(result);
BiFunction<String, String, String> concat = String::concat;
String stringResult = concat.apply("Bacon", "Ham");
System.out.println(stringResult);
assertNotNull(stringResult);
BiFunction<Integer, Integer, Integer> max = Integer::max;
int intResult = max.apply(1, 10);
System.out.println(intResult);
assertEquals(10, intResult);
BiFunction<String, String, Boolean> equals = Objects::equals;
boolean booleanResult = equals.apply("Bacon", "Ham");
System.out.println(booleanResult);
assertFalse(booleanResult);
}
zh_TW
BaconHam
10
false
methodParameter
Functional Interface BiFunction in Java 建立一個 BiFunction ,當作其他方法的傳入參數,執行程式邏輯,傳回物件。
public static double calc(double d1, double d2, BiFunction<Double, Double, Double> biFunction) {
return biFunction.apply(d1, d2);
}
public static double foodCalc(Food o1, Food o2, BiFunction<Food, Food, Double> biFunction) {
return biFunction.apply(o1, o2);
}
@Test
public void methodParameter() {
double result = calc(1d, 10d, (d1, d2) -> d1 + d2);
System.out.println(result);
assertEquals(11d, result, 2);
result = foodCalc(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), (o1, o2) -> o1.quantity + o2.quantity);
System.out.println(result);
assertEquals(3d, result, 2);
}
11.0
3.0
BiFunctionFunctionalTest.java
Functional Interface BiFunction in Java 新增單元測試,驗證 Java BiFunction Functional Interface 是否符合預期。
package org.ruoxue.java_147.functional.bifunction;
import static org.junit.Assert.*;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
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 BiFunctionFunctionalTest {
@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 methodReference() {
BiFunction<String, String, Locale> locale = Locale::new;
Locale result = locale.apply("zh", "TW");
System.out.println(result);
assertNotNull(result);
BiFunction<String, String, String> concat = String::concat;
String stringResult = concat.apply("Bacon", "Ham");
System.out.println(stringResult);
assertNotNull(stringResult);
BiFunction<Integer, Integer, Integer> max = Integer::max;
int intResult = max.apply(1, 10);
System.out.println(intResult);
assertEquals(10, intResult);
BiFunction<String, String, Boolean> equals = Objects::equals;
boolean booleanResult = equals.apply("Bacon", "Ham");
System.out.println(booleanResult);
assertFalse(booleanResult);
}
public static double calc(double d1, double d2, BiFunction<Double, Double, Double> biFunction) {
return biFunction.apply(d1, d2);
}
public static double foodCalc(Food o1, Food o2, BiFunction<Food, Food, Double> biFunction) {
return biFunction.apply(o1, o2);
}
@Test
public void methodParameter() {
double result = calc(1d, 10d, (d1, d2) -> d1 + d2);
System.out.println(result);
assertEquals(11d, result, 2);
result = foodCalc(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), (o1, o2) -> o1.quantity + o2.quantity);
System.out.println(result);
assertEquals(3d, result, 2);
}
}
心得分享
Java BiFunction Functional Interface 除了傳統實作接口的方法,使用 Lambda 表達式實作功能,能讓程式碼更加簡潔與直接,大幅提高可讀性, Functional Interface BiFunction in Java 提供了幾種 BiFunction 常見方法的操作範例。