Table of Contents
ToggleJava Collectors toList Method
使用所提供的靜態方法轉換成 List , ToList Java Collectors 介紹常見的 toList 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
public static <T> Collector<T, ?, List<T>> toList() {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- collector
| +- CollectorsToListTest.java
單元測試
Collectors ToList Java 提供設定鍵與值轉換成 List。
toList
Collectors ToList Java 建立一個 Stream ,增加五個元素,轉換成 List 。
@Test
public void toList() {
int expectedSize = 5;
Stream<String> stream = Stream.of("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
List<String> result = stream.collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[Blueberry, Melon, Fig, Blueberry, Melon]
toCollection
Collectors ToList Java 建立一個 Stream ,增加五個元素,轉換成 List 。
@Test
public void toCollection() {
int expectedSize = 5;
Stream<String> stream = Stream.of("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
List<String> result = stream.collect(Collectors.toCollection(LinkedList::new));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[Blueberry, Melon, Fig, Blueberry, Melon]
integerToList
Collectors ToList Java 建立一個 Stream ,增加五個元素,轉換成 List 。
@Test
public void integerToList() {
int expectedSize = 5;
Stream<Integer> stream = Stream.of(1, 2, 3, 1, 2);
List<Integer> result = stream.collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[1, 2, 3, 1, 2]
arrayToList
Java Collectors ToList 建立一個陣列,增加五個元素,轉換成 Stream ,再轉換成 List 。
@Test
public void arrayToList() {
int expectedSize = 5;
int[] array = { 1, 2, 3, 1, 2 };
Stream<int[]> stream = Stream.of(array);
List<Integer> result = stream.flatMapToInt(Arrays::stream).boxed().collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[1, 2, 3, 1, 2]
stringToList
Java Collectors ToList 建立一個字串, 取得 IntStream ,轉換成 List 。
@Test
public void stringToList() {
int expectedSize = 9;
String value = "Blueberry";
IntStream stream = value.chars();
List<Character> result = stream.mapToObj(e -> (char) e).collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
[B, l, u, e, b, e, r, r, y]
CollectorsToListTest.java
Collectors ToList Example 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.collector;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.Test;
public class CollectorsToListTest {
@Test
public void toList() {
int expectedSize = 5;
Stream<String> stream = Stream.of("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
List<String> result = stream.collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void toCollection() {
int expectedSize = 5;
Stream<String> stream = Stream.of("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
List<String> result = stream.collect(Collectors.toCollection(LinkedList::new));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void integerToList() {
int expectedSize = 5;
Stream<Integer> stream = Stream.of(1, 2, 3, 1, 2);
List<Integer> result = stream.collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void arrayToList() {
int expectedSize = 5;
int[] array = { 1, 2, 3, 1, 2 };
Stream<int[]> stream = Stream.of(array);
List<Integer> result = stream.flatMapToInt(Arrays::stream).boxed().collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void stringToList() {
int expectedSize = 9;
String value = "Blueberry";
IntStream stream = value.chars();
List<Character> result = stream.mapToObj(e -> (char) e).collect(Collectors.toList());
System.out.println(result);
assertEquals(expectedSize, result.size());
}
}
心得分享
Java Collectors ToList 提供了幾種 Collectors 常見方法的操作範例,在應用上相當廣泛,熟悉 Collectors ToList Example 這些方法的操作,例如: toList 等方法,可以讓程式碼更加地簡潔及容易維護。