Java Function Interface - Java 147

Java Function Interface – Java 147

Java Function Interface

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

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

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- functional
       |                   +- function
       |                       +- FunctionInterfaceTest.java   

單元測試

Function Interface Java 提供 Collection 中的 toMap 、 groupingBy 與 Stream 中的 map 、 flatMap 等方法操作 Function Interface in Java 。

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

Optional_map

Function Interface Java 建立 Function ,建立 Optional 容器,轉換容器內的值,傳回 Optional。

	@Test
	public void Optional_map() {
		Optional<String> opt = Optional.ofNullable("Bacon");
		Function<String, Integer> length = s -> s.length();
		Optional<Integer> lengthOpt = opt.map(length);
		int result = lengthOpt.orElse(0);
		System.out.println(result);
		assertEquals(5, result);

		Optional<Food> foodOpt = Optional.ofNullable(new Food("Ham", 1, 1));
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Boolean> lengthLessThan = i -> i > 1;
		Optional<Boolean> booleanOpt = foodOpt.map(foodLength.andThen(lengthLessThan));
		boolean booleanResult = booleanOpt.orElse(false);
		System.out.println(booleanResult);
		assertTrue(booleanResult);
	}
5
true

Optional_flatMap

Function Interface Java 建立 Function ,建立 Optional 容器,轉換容器內的值,傳回 Optional 。

	@Test
	public void Optional_flatMap() {
		Optional<String> opt = Optional.ofNullable("Bacon");
		Function<String, Optional<String>> toUpperCase = s -> Optional.ofNullable(s.toUpperCase());
		Optional<Optional<String>> mapOpt = opt.map(toUpperCase);
		System.out.println(mapOpt);
		assertEquals(Optional.ofNullable(Optional.ofNullable("BACON")), mapOpt);

		Optional<String> flatMapOpt = opt.flatMap(toUpperCase);
		System.out.println(flatMapOpt);
		assertEquals(Optional.ofNullable("BACON"), flatMapOpt);
	}
Optional[Optional[BACON]]
Optional[BACON]

Stream_map

Function Interface Java 建立 Function ,建立 Stream ,轉換流內的值,傳回 Stream 。

	@Test
	public void Stream_map() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		Function<String, Integer> length = String::length;
		Stream<Integer> stream = list.stream().map(length);
		long count = stream.peek(System.out::println).count();
		assertEquals(expectedSize, count);

		List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Integer> twice = i -> i * i;
		stream = foodList.stream().map(foodLength.andThen(twice));
		count = stream.peek(System.out::println).count();
		assertEquals(expectedSize, count);
	}
5
3
4
25
9
16

Stream_flatMap

Function Interface Java 建立 Function ,建立 Stream ,轉換流內的值,傳回 Stream 。

	@Test
	public void Stream_flatMap() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		Function<String, String> toUpperCase = String::toUpperCase;
		List<String> result = list.stream().map(toUpperCase).collect(Collectors.toList());
		System.out.println(result);
		assertEquals(expectedSize, result.size());

		List<List<String>> nestedlist = Arrays.asList(Arrays.asList("Bacon"), Arrays.asList("Ham"),
				Arrays.asList("Pork"));
		List<String> nestedResult = nestedlist.stream().flatMap(Collection::stream).collect(Collectors.toList());
		System.out.println(nestedResult);
		assertEquals(expectedSize, nestedResult.size());
	}
[BACON, HAM, PORK]
[Bacon, Ham, Pork]

Map_computeIfAbsent

Function Interface in Java 建立 Function ,建立一個 HashMap ,內有三個元素,計算不存在的 Key 新值。

	@Test
	public void Map_computeIfAbsent() {
		Integer expected = 5;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		Function<String, Integer> length = s -> s.length();
		Integer result = map.computeIfAbsent("Bread", length);
		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);
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Integer> twice = i -> i * i;
		result = foodMap.computeIfAbsent(new Food("Bread", 1, 1), foodLength.andThen(twice));
		System.out.println(foodMap);
		assertEquals(new Integer(25), result);
	}
{Ham=2, Bacon=1, Pork=3, Bread=5}
{{"name":"Bacon","quantity":1.0,"type":1}=1, {"name":"Ham","quantity":2.0,"type":1}=2, {"name":"Bread","quantity":1.0,"type":1}=25, {"name":"Pork","quantity":3.0,"type":1}=3}

Collectors_toMap

Function Interface in Java 建立 Function ,建立一個 List ,內有三個元素,轉換成 Map , Java Function Interface Example 提供範例。

	@Test
	public void Collectors_toMap() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		Function<String, String> key = s -> s.toUpperCase();
		Function<String, Integer> length = s -> s.length();
		Map<String, Integer> map = list.stream().collect(Collectors.toMap(key, length));
		System.out.println(map);
		assertEquals(expectedSize, map.size());

		List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
		Function<Food, String> foodKey = o -> o.name;
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Integer> twice = i -> i * i;
		Map<String, Integer> foodMap = foodList.stream().collect(Collectors.toMap(foodKey, foodLength.andThen(twice)));
		System.out.println(foodMap);
		assertEquals(expectedSize, foodMap.size());
	}
{BACON=5, HAM=3, PORK=4}
{Ham=9, Bacon=25, Pork=16}

Collectors_groupingBy

Function Interface in Java 建立 Function ,內有四個元素,根據屬性對元素分組,傳回 Map , Java Function Interface Example 提供範例。

	@Test
	public void Collectors_groupingBy() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork", "Bread");
		Function<String, Integer> length = s -> s.length();
		Map<Integer, List<String>> map = list.stream().collect(Collectors.groupingBy(length));
		System.out.println(map);
		assertEquals(expectedSize, map.size());

		List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 2),
				new Food("Bread", 4, 3));
		Map<Integer, List<Food>> foodMap = foodList.stream().collect(Collectors.groupingBy(Food::getType));
		System.out.println(foodMap);
		assertEquals(expectedSize, foodMap.size());
	}
{3=[Ham], 4=[Pork], 5=[Bacon, Bread]}
{1=[{"name":"Bacon","quantity":1.0,"type":1}, {"name":"Ham","quantity":2.0,"type":1}], 2=[{"name":"Pork","quantity":3.0,"type":2}], 3=[{"name":"Bread","quantity":4.0,"type":3}]}

FunctionInterfaceTest.java

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

package org.ruoxue.java_147.functional.function;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

	@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 Optional_map() {
		Optional<String> opt = Optional.ofNullable("Bacon");
		Function<String, Integer> length = s -> s.length();
		Optional<Integer> lengthOpt = opt.map(length);
		int result = lengthOpt.orElse(0);
		System.out.println(result);
		assertEquals(5, result);

		Optional<Food> foodOpt = Optional.ofNullable(new Food("Ham", 1, 1));
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Boolean> lengthLessThan = i -> i > 1;
		Optional<Boolean> booleanOpt = foodOpt.map(foodLength.andThen(lengthLessThan));
		boolean booleanResult = booleanOpt.orElse(false);
		System.out.println(booleanResult);
		assertTrue(booleanResult);
	}

	@Test
	public void Optional_flatMap() {
		Optional<String> opt = Optional.ofNullable("Bacon");
		Function<String, Optional<String>> toUpperCase = s -> Optional.ofNullable(s.toUpperCase());
		Optional<Optional<String>> mapOpt = opt.map(toUpperCase);
		System.out.println(mapOpt);
		assertEquals(Optional.ofNullable(Optional.ofNullable("BACON")), mapOpt);

		Optional<String> flatMapOpt = opt.flatMap(toUpperCase);
		System.out.println(flatMapOpt);
		assertEquals(Optional.ofNullable("BACON"), flatMapOpt);
	}

	@Test
	public void Stream_map() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		Function<String, Integer> length = String::length;
		Stream<Integer> stream = list.stream().map(length);
		long count = stream.peek(System.out::println).count();
		assertEquals(expectedSize, count);

		List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Integer> twice = i -> i * i;
		stream = foodList.stream().map(foodLength.andThen(twice));
		count = stream.peek(System.out::println).count();
		assertEquals(expectedSize, count);
	}

	@Test
	public void Stream_flatMap() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		Function<String, String> toUpperCase = String::toUpperCase;
		List<String> result = list.stream().map(toUpperCase).collect(Collectors.toList());
		System.out.println(result);
		assertEquals(expectedSize, result.size());

		List<List<String>> nestedlist = Arrays.asList(Arrays.asList("Bacon"), Arrays.asList("Ham"),
				Arrays.asList("Pork"));
		List<String> nestedResult = nestedlist.stream().flatMap(Collection::stream).collect(Collectors.toList());
		System.out.println(nestedResult);
		assertEquals(expectedSize, nestedResult.size());
	}

	@Test
	public void Map_computeIfAbsent() {
		Integer expected = 5;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Bacon", 1);
		map.put("Ham", 2);
		map.put("Pork", 3);
		Function<String, Integer> length = s -> s.length();
		Integer result = map.computeIfAbsent("Bread", length);
		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);
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Integer> twice = i -> i * i;
		result = foodMap.computeIfAbsent(new Food("Bread", 1, 1), foodLength.andThen(twice));
		System.out.println(foodMap);
		assertEquals(new Integer(25), result);
	}

	@Test
	public void Collectors_toMap() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
		Function<String, String> key = s -> s.toUpperCase();
		Function<String, Integer> length = s -> s.length();
		Map<String, Integer> map = list.stream().collect(Collectors.toMap(key, length));
		System.out.println(map);
		assertEquals(expectedSize, map.size());

		List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
		Function<Food, String> foodKey = o -> o.name;
		Function<Food, Integer> foodLength = o -> o.name.length();
		Function<Integer, Integer> twice = i -> i * i;
		Map<String, Integer> foodMap = foodList.stream().collect(Collectors.toMap(foodKey, foodLength.andThen(twice)));
		System.out.println(foodMap);
		assertEquals(expectedSize, foodMap.size());
	}

	@Test
	public void Collectors_groupingBy() {
		int expectedSize = 3;
		List<String> list = Arrays.asList("Bacon", "Ham", "Pork", "Bread");
		Function<String, Integer> length = s -> s.length();
		Map<Integer, List<String>> map = list.stream().collect(Collectors.groupingBy(length));
		System.out.println(map);
		assertEquals(expectedSize, map.size());

		List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 2),
				new Food("Bread", 4, 3));
		Map<Integer, List<Food>> foodMap = foodList.stream().collect(Collectors.groupingBy(Food::getType));
		System.out.println(foodMap);
		assertEquals(expectedSize, foodMap.size());
	}
}

心得分享

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

發佈留言