Table of Contents
ToggleFunction Functional Interface in Java
可當作其他方法的傳入參數或是引用其他方法為實例,使用 Lambda 語法,傳入 1 個泛型物件參數,結果傳回泛型物件,可以使用 andThen 、 compose 組合成鏈式判斷, Functional Interface Function 介紹常見的方法引用、方法參數等操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- function
| +- FunctionFunctionalTest.java
單元測試
Function Functional Interface 提供方法引用、參考等操作 Java Function 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
Function Functional Interface 建立一個 Function ,引用其他方法為實例,轉換元素。
@Test
public void methodReference() {
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<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
Function<Food, String> getName = Food::getName;
Function<String, String> toLowerCase = String::toLowerCase;
result = foodList.stream().map(getName.andThen(toLowerCase)).collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[BACON, HAM, PORK]
[bacon, ham, pork]
methodParameter
Functional Interface Function in Java 建立一個 Function ,當作其他方法的傳入參數,轉換元素。
public static List<String> map(List<String> list, Function<String, String> function) {
return list.stream().map(function).collect(Collectors.toList());
}
public static List<String> foodMap(List<Food> list, Function<Food, String> function) {
return list.stream().map(function).collect(Collectors.toList());
}
@Test
public void methodParameter() {
int expectedSize = 3;
List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
Function<String, String> toUpperCase = s -> s.toUpperCase();
List<String> result = map(list, toUpperCase);
System.out.println(result);
assertEquals(expectedSize, result.size());
List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
Function<Food, String> getName = o -> o.name;
Function<String, String> toLowerCase = s -> s.toUpperCase();
result = foodMap(foodList, getName.andThen(toLowerCase));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[BACON, HAM, PORK]
[BACON, HAM, PORK]
FunctionFunctionalTest.java
Functional Interface Function in Java 新增單元測試,驗證 Java Function Functional Interface 是否符合預期。
package org.ruoxue.java_147.functional.function;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
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 FunctionFunctionalTest {
@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() {
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<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
Function<Food, String> getName = Food::getName;
Function<String, String> toLowerCase = String::toLowerCase;
result = foodList.stream().map(getName.andThen(toLowerCase)).collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
public static List<String> map(List<String> list, Function<String, String> function) {
return list.stream().map(function).collect(Collectors.toList());
}
public static List<String> foodMap(List<Food> list, Function<Food, String> function) {
return list.stream().map(function).collect(Collectors.toList());
}
@Test
public void methodParameter() {
int expectedSize = 3;
List<String> list = Arrays.asList("Bacon", "Ham", "Pork");
Function<String, String> toUpperCase = s -> s.toUpperCase();
List<String> result = map(list, toUpperCase);
System.out.println(result);
assertEquals(expectedSize, result.size());
List<Food> foodList = Arrays.asList(new Food("Bacon", 1, 1), new Food("Ham", 2, 1), new Food("Pork", 3, 1));
Function<Food, String> getName = o -> o.name;
Function<String, String> toLowerCase = s -> s.toUpperCase();
result = foodMap(foodList, getName.andThen(toLowerCase));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
}
心得分享
Java Function Functional Interface 除了傳統實作接口的方法,使用 Lambda 表達式實作功能,能讓程式碼更加簡潔與直接,大幅提高可讀性, Functional Interface Function in Java 提供了幾種 Function 常見方法的操作範例。