Table of Contents
ToggleJava Consumer Method Reference
引用其他方法為實例,使用 Lambda 語法,傳入 1 個泛型物件參數,無傳回值,例如引用 StringBuilder 、 List 、 Map 、 Queue 等, Consumer Method Reference Java 介紹常見的 append 、 add 、 remove 、 offer 等方法,了解 Consumer 的不同操作和應用,本篇增加了範例,並透過單元測試來驗證產出結果。
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- functional
| +- consumer
| +- ConsumerWithExamplesTest.java
單元測試
Consumer Java Method Reference 提供方法引用、參考等操作 Consumer accept Java 。
StringBuilder_append
建立一個 Consumer ,使用 StringBuilder append ,新增字串。
@Test
public void StringBuilder_append() {
StringBuilder value = new StringBuilder();
Consumer<String> append = value::append;
append.accept("Bacon, ");
append.accept("Ham, ");
append.accept("Pork");
System.out.println(value);
}
Bacon, Ham, Pork
List_add
Consumer Java Method Reference 建立一個 Consumer ,使用 List add ,增加三個元素。
@Test
public void List_add() {
int expectedSize = 3;
List<String> list = new ArrayList<String>();
Consumer<String> add = list::add;
add.accept("Bacon");
add.accept("Ham");
add.accept("Pork");
System.out.println(list);
assertEquals(expectedSize, list.size());
}
List_remove
[Bacon, Ham, Pork]
Consumer Java Method Reference 建立一個 Consumer ,使用 List remove ,內有三個元素,刪除指定元素。
@Test
public void List_remove() {
int expectedSize = 2;
List<String> list = new ArrayList<String>(Arrays.asList("Bacon", "Ham", "Pork"));
Consumer<String> remove = list::remove;
remove.accept("Bacon");
remove.accept("Bread");
System.out.println(list);
assertEquals(expectedSize, list.size());
}
[Ham, Pork]
Map_remove
Consumer Java Method Reference 建立一個 Consumer ,使用 Map remove ,內有三個元素,刪除指定元素。
@Test
public void Map_remove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Bacon", 1);
map.put("Ham", 2);
map.put("Pork", 3);
Consumer<String> remove = map::remove;
remove.accept("Bacon");
remove.accept("Bread");
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Ham=2, Pork=3}
Queue_offer
Java Consumer accept 建立一個 Consumer ,使用 Queue offer ,增加三個元素。
@Test
public void Queue_offer() {
int expectedSize = 3;
Queue<String> queue = new ConcurrentLinkedQueue<String>();
Consumer<String> offer = queue::offer;
offer.accept("Bacon");
offer.accept("Ham");
Consumer<String> queueOfferConsumer = queue::offer;
queueOfferConsumer.accept("Pork");
System.out.println(queue);
assertEquals(expectedSize, queue.size());
}
[Bacon, Ham, Pork]
Queue_remove
Java Consumer accept 建立一個 Consumer ,使用 Queue remove ,內有三個元素,刪除指定元素。
@Test
public void Queue_remove() {
int expectedSize = 2;
Queue<String> queue = new ConcurrentLinkedQueue<String>(Arrays.asList("Bacon", "Ham", "Pork"));
Consumer<String> remove = queue::remove;
remove.accept("Bacon");
remove.accept("Bread");
System.out.println(queue);
assertEquals(expectedSize, queue.size());
}
[Ham, Pork]
ConsumerReferenceTest.java
Java Consumer accept 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.functional.consumer;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import org.junit.Test;
public class ConsumerReferenceTest {
@Test
public void StringBuilder_append() {
StringBuilder value = new StringBuilder();
Consumer<String> append = value::append;
append.accept("Bacon, ");
append.accept("Ham, ");
append.accept("Pork");
System.out.println(value);
}
@Test
public void List_add() {
int expectedSize = 3;
List<String> list = new ArrayList<String>();
Consumer<String> add = list::add;
add.accept("Bacon");
add.accept("Ham");
add.accept("Pork");
System.out.println(list);
assertEquals(expectedSize, list.size());
}
@Test
public void List_remove() {
int expectedSize = 2;
List<String> list = new ArrayList<String>(Arrays.asList("Bacon", "Ham", "Pork"));
Consumer<String> remove = list::remove;
remove.accept("Bacon");
remove.accept("Bread");
System.out.println(list);
assertEquals(expectedSize, list.size());
}
@Test
public void Map_remove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Bacon", 1);
map.put("Ham", 2);
map.put("Pork", 3);
Consumer<String> remove = map::remove;
remove.accept("Bacon");
remove.accept("Bread");
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void Queue_offer() {
int expectedSize = 3;
Queue<String> queue = new ConcurrentLinkedQueue<String>();
Consumer<String> offer = queue::offer;
offer.accept("Bacon");
offer.accept("Ham");
Consumer<String> queueOfferConsumer = queue::offer;
queueOfferConsumer.accept("Pork");
System.out.println(queue);
assertEquals(expectedSize, queue.size());
}
@Test
public void Queue_remove() {
int expectedSize = 2;
Queue<String> queue = new ConcurrentLinkedQueue<String>(Arrays.asList("Bacon", "Ham", "Pork"));
Consumer<String> remove = queue::remove;
remove.accept("Bacon");
remove.accept("Bread");
System.out.println(queue);
assertEquals(expectedSize, queue.size());
}
}
心得分享
Consumer accept Java 除了傳統實作接口的方法,使用 Lambda 表達式實作功能,能讓程式碼更加簡潔與直接,大幅提高可讀性, Java Consumer accept 提供了幾種 Consumer 常見方法的操作範例。