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