BiFunction in Java with Examples - Java 147

BiFunction in Java with Examples – Java 147

BiFunction in Java with Examples

只有一個抽象方法的接口,定義了 apply 方法,可以在一個元素上測試或應用一些操作,常用於物件轉換或數字運算,取代傳統實作接口的方法,讓程式碼更加簡潔和易讀, BiFunction in Java 本篇增加了範例,並透過單元測試來驗證產出結果。

@FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);
}

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- bifunctional
       |                   +- bifunction
       |                       +- BiFunctionWithExamplesTest.java   

單元測試

BiFunction Java 提供 apply 、 andThen 條件或組合成鏈式判斷等操作 BiFunction 。

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();
		}
	}	

apply

建立 BiFunction 物件,傳入 2 個參數,執行程式邏輯,傳回物件。

	@Test
	public void apply() {
		BiFunction<Food, String, Boolean> startsWith = (o, s) -> o.name.startsWith(s);
		Food food = new Food("Bacon", 1, 1);
		boolean result = startsWith.apply(food, "B");
		System.out.println(result);
		assertTrue(result);
		food = new Food("Ham", 3, 1);
		result = startsWith.apply(food, "B");
		System.out.println(result);
		assertFalse(result);

		BiFunction<Food, Integer, Boolean> greaterThan = (o, i) -> o.name.length() > i;
		result = greaterThan.apply(new Food("Bacon", 1, 1), 3);
		System.out.println(result);
		assertTrue(result);
		result = greaterThan.apply(new Food("Ham", 3, 1), 3);
		System.out.println(result);
		assertFalse(result);
	}
true
false
true
false

andThen

BiFunction Java Methods 建立 1 個 BiFunction 物件,傳入 2 個參數,使用 andThen 組合執行程式邏輯,傳回物件,其中若有例外拋出,將會中斷執行。

	@Test
	public void andThen() {
		BiFunction<Food, String, String> concat = (o, s) -> o.name.concat(s);
		Function<String, Integer> multiply = s -> s.length() * 2;
		Food food = new Food("Bacon", 1, 1);
		int result = concat.andThen(multiply).apply(food, "B");
		System.out.println(result);
		assertEquals(12, result);

		BiFunction<Food, Integer, Integer> length = (o, i) -> o.name.length() + i;
		Function<Integer, Integer> twice = i -> i * i;
		int intResult = length.andThen(twice).apply(new Food("Ham", 3, 1), 1);
		System.out.println(intResult);
		assertEquals(16, intResult);
	}
12
16

andThenThrowException

BiFunction Java Methods 建立 BiFunction 物件,傳入參數 null ,會拋出例外 。

	@Test(expected = NullPointerException.class)
	public void andThenThrowException() {
		BiFunction<Food, String, String> concat = (o, s) -> o.name.concat(s);
		concat = concat.andThen(null);
	}
java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at java.util.function.BiFunction.andThen(BiFunction.java:69)
	at org.ruoxue.java_147.functional.bifunction.BiFunctionWithExamplesTest.andThenThrowException(BiFunctionWithExamplesTest.java:91)

traditional

BiFunction Methods in Java 使用傳統方式,實作 BiFunction 接口,傳回物件。

	public static class Concat<E> implements BiFunction<Food, String, String> {
		@Override
		public String apply(Food t, String u) {
			return t.name.concat(u);
		}
	}

	@Test
	public void traditional() {
		BiFunction<Food, String, String> concat = new Concat<>();
		Function<String, Integer> multiply = s -> s.length() * 2;
		Food food = new Food("Bacon", 1, 1);
		int result = concat.andThen(multiply).apply(food, "B");
		System.out.println(result);
		assertEquals(12, result);
		food = new Food("Ham", 2, 1);
		result = concat.andThen(multiply).apply(food, "B");
		System.out.println(result);
		assertEquals(8, result);
	}
12
8

BiFunctionWithExamplesTest.java

BiFunction in Java 新增單元測試,驗證 Java BiFunction Example 是否符合預期。

package org.ruoxue.java_147.functional.bifunction;

import static org.junit.Assert.*;

import java.util.function.BiFunction;
import java.util.function.Function;

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 BiFunctionWithExamplesTest {

	@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 apply() {
		BiFunction<Food, String, Boolean> startsWith = (o, s) -> o.name.startsWith(s);
		Food food = new Food("Bacon", 1, 1);
		boolean result = startsWith.apply(food, "B");
		System.out.println(result);
		assertTrue(result);
		food = new Food("Ham", 3, 1);
		result = startsWith.apply(food, "B");
		System.out.println(result);
		assertFalse(result);

		BiFunction<Food, Integer, Boolean> greaterThan = (o, i) -> o.name.length() > i;
		result = greaterThan.apply(new Food("Bacon", 1, 1), 3);
		System.out.println(result);
		assertTrue(result);
		result = greaterThan.apply(new Food("Ham", 3, 1), 3);
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void andThen() {
		BiFunction<Food, String, String> concat = (o, s) -> o.name.concat(s);
		Function<String, Integer> multiply = s -> s.length() * 2;
		Food food = new Food("Bacon", 1, 1);
		int result = concat.andThen(multiply).apply(food, "B");
		System.out.println(result);
		assertEquals(12, result);

		BiFunction<Food, Integer, Integer> length = (o, i) -> o.name.length() + i;
		Function<Integer, Integer> twice = i -> i * i;
		int intResult = length.andThen(twice).apply(new Food("Ham", 3, 1), 1);
		System.out.println(intResult);
		assertEquals(16, intResult);
	}

	@Test(expected = NullPointerException.class)
	public void andThenThrowException() {
		BiFunction<Food, String, String> concat = (o, s) -> o.name.concat(s);
		concat = concat.andThen(null);
	}

	public static class Concat<E> implements BiFunction<Food, String, String> {
		@Override
		public String apply(Food t, String u) {
			return t.name.concat(u);
		}
	}

	@Test
	public void traditional() {
		BiFunction<Food, String, String> concat = new Concat<>();
		Function<String, Integer> multiply = s -> s.length() * 2;
		Food food = new Food("Bacon", 1, 1);
		int result = concat.andThen(multiply).apply(food, "B");
		System.out.println(result);
		assertEquals(12, result);
		food = new Food("Ham", 2, 1);
		result = concat.andThen(multiply).apply(food, "B");
		System.out.println(result);
		assertEquals(8, result);
	}
}

心得分享

Java BiFunction Example 功能接口,使用 Lambda 語法,可當作其他方法的傳入參數或是引用其他方法為實例, Java BiFunction 提供了幾種 BiFunction 常見方法的操作範例,例如: apply 、 andThen 等方法。

發佈留言