Java BiFunction Interface - Java 147

Java BiFunction Interface – Java 147

Java BiFunction Interface

常用於物件轉換或數字運算,也可當作其他方法的傳入參數或是引用其他方法為實例, BiFunction Interface 介紹 Map 中的 compute 、 merge 、 replaceAll 等方法,了解 BiFunction 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。

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

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- functional
       |                   +- bifunction
       |                       +- BiFunctionInterfaceTest.java   

單元測試

BiFunction Interface Java 提供 Map 中的 compute 、 merge 、 replaceAll 等方法操作 BiFunction Interface 。

Food

Function Interface Java 建立 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();
		}
	}

Map_compute

BiFunction Interface Java 建立 BiFunction ,建立一個 HashMap ,增加三個元素,計算符合條件的新值。

	@Test
	public void Map_compute() {
		Integer expected = 7;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		String key = "Bacon";
		BiFunction<String, Integer, Integer> addition = (s, i) -> s.length() + i + 1;
		Integer result = map.compute(key, addition);
		System.out.println(result);
		System.out.println(map);
		assertEquals(expected, result);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Food, Integer, Integer> multiply = (o, i) -> (int) (o.name.length() + o.quantity * 2);
		Food foodKey = new Food("Bacon", 1, 1);
		Integer foodResult = foodMap.compute(foodKey, multiply);
		System.out.println(foodResult);
		assertEquals(expected, foodResult);
		System.out.println(foodMap);
	}
7
{Ham=2, Bacon=7, Pork=3}
7
{{"name":"Bacon","quantity":1.0,"type":1}=7, {"name":"Ham","quantity":2.0,"type":1}=2, {"name":"Pork","quantity":3.0,"type":1}=3}

Map_computeIfPresent

BiFunction Interface Java 建立 BiFunction ,建立一個 HashMap ,內有三個元素,計算已存在的 Key 新值, Java BiFunction Interface Example 提供範例參考。

	@Test
	public void Map_computeIfPresent() {
		Integer expected = 2;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		String key = "Bacon";
		BiFunction<String, Integer, Integer> addition = (s, i) -> i + 1;
		Integer result = map.computeIfPresent(key, addition);
		System.out.println(result);
		assertEquals(expected, result);
		key = "Bread";
		result = map.computeIfPresent(key, addition);
		System.out.println(result);
		assertNull(result);
		System.out.println(map);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Food, Integer, Integer> multiply = (o, i) -> (int) o.quantity * 2;
		Food foodKey = new Food("Bacon", 1, 1);
		Integer foodResult = foodMap.computeIfPresent(foodKey, multiply);
		System.out.println(foodResult);
		assertEquals(expected, foodResult);
		foodKey = new Food("Bread", 1, 1);
		foodResult = foodMap.computeIfPresent(foodKey, multiply);
		System.out.println(foodResult);
		assertNull(foodResult);
		System.out.println(foodMap);
	}
2
null
{Ham=2, Bacon=2, Pork=3}
2
null
{{"name":"Bacon","quantity":1.0,"type":1}=2, {"name":"Ham","quantity":2.0,"type":1}=2, {"name":"Pork","quantity":3.0,"type":1}=3}

Map_merge

BiFunction Interface in Java 建立 BiFunction ,建立一個 HashMap ,內有三個元素,指定 key 值,合併 value 舊值與新值, Java BiFunction Interface Example 提供範例參考。

	@Test
	public void Map_merge() {
		Integer expected = 11;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		String key = "Bacon";
		BiFunction<Integer, Integer, Integer> addition = (i, i2) -> i + i2;
		Integer result = map.merge(key, 10, addition);
		System.out.println(result);
		assertEquals(expected, result);

		key = "Bread";
		result = map.merge(key, 11, addition);
		System.out.println(result);
		assertEquals(expected, result);
		System.out.println(map);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Integer, Integer, Integer> multiply = (i, i2) -> (int) i * i2;
		Food foodKey = new Food("Bacon", 1, 1);
		Integer foodResult = foodMap.merge(foodKey, 11, multiply);
		System.out.println(foodMap);
		assertEquals(expected, foodResult);
	}
11
11
{Ham=2, Bacon=11, Pork=3, Bread=11}
{{"name":"Bacon","quantity":1.0,"type":1}=11, {"name":"Ham","quantity":2.0,"type":1}=2, {"name":"Pork","quantity":3.0,"type":1}=3}

Map_replaceAll

BiFunction Interface in Java 建立 BiFunction ,建立一個 HashMap ,內有三個元素,取代所有元素的值, Java BiFunction Interface Example 提供範例參考。

	@Test
	public void Map_replaceAll() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		BiFunction<String, Integer, Integer> length = (s, i) -> s.length() + i;
		map.replaceAll(length);
		System.out.println(map);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Food, Integer, Integer> multiply = (o, i) -> (int) o.name.length() * i;
		foodMap.replaceAll(multiply);
		System.out.println(foodMap);
	}
{Ham=5, Bacon=6, Pork=7}
{{"name":"Bacon","quantity":1.0,"type":1}=5, {"name":"Ham","quantity":2.0,"type":1}=6, {"name":"Pork","quantity":3.0,"type":1}=12}

BiFunctionInterfaceTest.java

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

package org.ruoxue.java_147.functional.bifunction;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;
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 BiFunctionInterfaceTest {

	@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 Map_compute() {
		Integer expected = 7;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		String key = "Bacon";
		BiFunction<String, Integer, Integer> addition = (s, i) -> s.length() + i + 1;
		Integer result = map.compute(key, addition);
		System.out.println(result);
		System.out.println(map);
		assertEquals(expected, result);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Food, Integer, Integer> multiply = (o, i) -> (int) (o.name.length() + o.quantity * 2);
		Food foodKey = new Food("Bacon", 1, 1);
		Integer foodResult = foodMap.compute(foodKey, multiply);
		System.out.println(foodResult);
		assertEquals(expected, foodResult);
		System.out.println(foodMap);
	}

	@Test
	public void Map_computeIfPresent() {
		Integer expected = 2;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		String key = "Bacon";
		BiFunction<String, Integer, Integer> addition = (s, i) -> i + 1;
		Integer result = map.computeIfPresent(key, addition);
		System.out.println(result);
		assertEquals(expected, result);
		key = "Bread";
		result = map.computeIfPresent(key, addition);
		System.out.println(result);
		assertNull(result);
		System.out.println(map);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Food, Integer, Integer> multiply = (o, i) -> (int) o.quantity * 2;
		Food foodKey = new Food("Bacon", 1, 1);
		Integer foodResult = foodMap.computeIfPresent(foodKey, multiply);
		System.out.println(foodResult);
		assertEquals(expected, foodResult);
		foodKey = new Food("Bread", 1, 1);
		foodResult = foodMap.computeIfPresent(foodKey, multiply);
		System.out.println(foodResult);
		assertNull(foodResult);
		System.out.println(foodMap);
	}

	@Test
	public void Map_merge() {
		Integer expected = 11;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		String key = "Bacon";
		BiFunction<Integer, Integer, Integer> addition = (i, i2) -> i + i2;
		Integer result = map.merge(key, 10, addition);
		System.out.println(result);
		assertEquals(expected, result);

		key = "Bread";
		result = map.merge(key, 11, addition);
		System.out.println(result);
		assertEquals(expected, result);
		System.out.println(map);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Integer, Integer, Integer> multiply = (i, i2) -> (int) i * i2;
		Food foodKey = new Food("Bacon", 1, 1);
		Integer foodResult = foodMap.merge(foodKey, 11, multiply);
		System.out.println(foodMap);
		assertEquals(expected, foodResult);
	}

	@Test
	public void Map_replaceAll() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		BiFunction<String, Integer, Integer> length = (s, i) -> s.length() + i;
		map.replaceAll(length);
		System.out.println(map);

		Map<Food, Integer> foodMap = new HashMap<Food, Integer>();
		foodMap.put(new Food("Bacon", 1, 1), 1);
		foodMap.put(new Food("Ham", 2, 1), 2);
		foodMap.put(new Food("Pork", 3, 1), 3);
		BiFunction<Food, Integer, Integer> multiply = (o, i) -> (int) o.name.length() * i;
		foodMap.replaceAll(multiply);
		System.out.println(foodMap);
	}
}

心得分享

Java BiFunction Interface Example 使用 Lambda 表達式能讓程式碼更加簡潔與直接,取代傳統實作接口的方法,減少了很多程式碼,大幅提高可讀性, BiFunction Interface in Java 提供了幾種 BiFunction 常見方法的操作範例,熟悉這些方法的操作,能夠提高開發效率,節省維護上的成本。

發佈留言