Table of Contents
ToggleJava List Primitive Type Stream
提供 List 轉換成 Primitive Stream ,支援 Stream 排序、並行處理、聚合操作等,可以避免大量不必要的物件建立,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- stream
| +- ListPrimitiveTypeStreamsTest.java
單元測試
Primitive Type Streams API 是 Java 8 中增加的特性之一,允許通過提供聲明性 API 來方便且更有效地處理集合和其他元素序列。
mapToInt
建立一個 List,增加三個元素,使用 stream.mapToInt 將 List 轉成 IntStream,內有三個元素。
@Test
public void mapToInt() {
int expectedCount = 3;
List<Integer> list = Arrays.asList(1, 2, 3);
IntStream stream = list.stream().mapToInt(e -> e);
// stream.forEach(System.out::println);
long count = stream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
1
2
3
mapToLong
建立一個 List,增加三個元素,使用 stream.mapToLong 將 List 轉成 LongStream,內有三個元素。
@Test
public void mapToLong() {
int expectedCount = 3;
List<Long> list = Arrays.asList(4L, 5L, 6L);
LongStream stream = list.stream().mapToLong(e -> e);
// stream.forEach(System.out::println);
long count = stream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
4
5
6
mapToDouble
建立一個 List,增加三個元素,使用 stream.mapToDouble 將 List 轉成 DoubleStream,內有三個元素。
@Test
public void mapToDouble() {
int expectedCount = 3;
List<Double> list = Arrays.asList(7.0, 8.0, 9.0);
DoubleStream stream = list.stream().mapToDouble(e -> e);
// stream.forEach(System.out::println);
long count = stream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
7.0
8.0
9.0
sum
建立一個 IntStream,增加三個元素,計算總和。
@Test
public void sum() {
int expectedSum = 6;
IntStream stream = IntStream.of(1, 2, 3);
int sum = stream.sum();
System.out.println(sum);
assertEquals(expectedSum, sum);
}
6
average
建立一個 IntStream,增加三個元素,計算平均數。
@Test
public void average() {
int expectedAverage = 2;
IntStream stream = IntStream.of(1, 2, 3);
double average = stream.average().getAsDouble();
System.out.println(average);
assertEquals(expectedAverage, average, 0);
}
2
max
建立一個 IntStream,增加三個元素,取得最大數。
@Test
public void max() {
int expectedMax = 3;
IntStream stream = IntStream.of(1, 2, 3);
int max = stream.max().getAsInt();
System.out.println(max);
assertEquals(expectedMax, max);
}
3
min
建立一個 IntStream,增加三個元素,取得最小數。
@Test
public void min() {
int expectedMin = 1;
IntStream stream = IntStream.of(1, 2, 3);
int min = stream.min().getAsInt();
System.out.println(min);
assertEquals(expectedMin, min);
}
1
count
建立一個 IntStream,增加三個元素,取得元素個數。
@Test
public void count() {
int expectedCount = 3;
IntStream stream = IntStream.of(1, 2, 3);
long count = stream.count();
System.out.println(count);
assertEquals(expectedCount, count);
}
3
intermediate
建立一個 IntStream,增加三個元素,第一次調用 Stream 方法,然後再第二次調用時,會拋出 IllegalStateException 例外。
@Test(expected = IllegalStateException.class)
public void intermediate() {
int expectedMin = 1;
int expectedMax = 3;
IntStream stream = IntStream.of(1, 2, 3);
int max = stream.max().getAsInt();
System.out.println(max);
assertEquals(expectedMax, max);
int min = stream.min().getAsInt();
System.out.println(min);
assertEquals(expectedMin, min);
}
supplier
建立一個 IntStream,增加三個元素,使用 Supplier ,第一次調用 Stream 方法,然後再第二次調用時,不會拋出例外。
@Test
public void supplier() {
int expectedMin = 1;
int expectedMax = 3;
Supplier<IntStream> supplier = () -> IntStream.of(1, 2, 3);
int max = supplier.get().max().getAsInt();
System.out.println(max);
assertEquals(expectedMax, max);
int min = supplier.get().min().getAsInt();
System.out.println(min);
assertEquals(expectedMin, min);
}
ListPrimitiveTypeStreamsTest.java
List Primitive Type Streams 新增單元測試,驗證是否符合預期 。
package org.ruoxue.java_147.stream;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import org.junit.Test;
public class ListPrimitiveTypeStreamsTest {
public ListPrimitiveTypeStreamsTest() {
}
@Test
public void mapToInt() {
int expectedCount = 3;
List<Integer> list = Arrays.asList(1, 2, 3);
IntStream stream = list.stream().mapToInt(e -> e);
// stream.forEach(System.out::println);
long count = stream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
@Test
public void mapToLong() {
int expectedCount = 3;
List<Long> list = Arrays.asList(4L, 5L, 6L);
LongStream stream = list.stream().mapToLong(e -> e);
// stream.forEach(System.out::println);
long count = stream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
@Test
public void mapToDouble() {
int expectedCount = 3;
List<Double> list = Arrays.asList(7.0, 8.0, 9.0);
DoubleStream stream = list.stream().mapToDouble(e -> e);
// stream.forEach(System.out::println);
long count = stream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
@Test
public void sum() {
int expectedSum = 6;
IntStream stream = IntStream.of(1, 2, 3);
int sum = stream.sum();
System.out.println(sum);
assertEquals(expectedSum, sum);
}
@Test
public void average() {
int expectedAverage = 2;
IntStream stream = IntStream.of(1, 2, 3);
double average = stream.average().getAsDouble();
System.out.println(average);
assertEquals(expectedAverage, average, 0);
}
@Test
public void max() {
int expectedMax = 3;
IntStream stream = IntStream.of(1, 2, 3);
int max = stream.max().getAsInt();
System.out.println(max);
assertEquals(expectedMax, max);
}
@Test
public void min() {
int expectedMin = 1;
IntStream stream = IntStream.of(1, 2, 3);
int min = stream.min().getAsInt();
System.out.println(min);
assertEquals(expectedMin, min);
}
@Test
public void count() {
int expectedCount = 3;
IntStream stream = IntStream.of(1, 2, 3);
long count = stream.count();
System.out.println(count);
assertEquals(expectedCount, count);
}
@Test(expected = IllegalStateException.class)
public void intermediate() {
int expectedMin = 1;
int expectedMax = 3;
IntStream stream = IntStream.of(1, 2, 3);
int max = stream.max().getAsInt();
System.out.println(max);
assertEquals(expectedMax, max);
int min = stream.min().getAsInt();
System.out.println(min);
assertEquals(expectedMin, min);
}
@Test
public void supplier() {
int expectedMin = 1;
int expectedMax = 3;
Supplier<IntStream> supplier = () -> IntStream.of(1, 2, 3);
int max = supplier.get().max().getAsInt();
System.out.println(max);
assertEquals(expectedMax, max);
int min = supplier.get().min().getAsInt();
System.out.println(min);
assertEquals(expectedMin, min);
}
}
心得分享
Java Streams with Primitive Type 提供了從 List 轉成 3 種 基本型別 Stream 及 Supplier 支援多次調用 Stream 方法。