Table of Contents
ToggleJava Primitive Type Stream Boxed
提供 Primitive Stream 轉換成 Object Stream ,支援 Stream 排序、並行處理、聚合操作等,可以避免大量不必要的物件建立,節省系統資源,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- stream
| +- PrimitiveStreamBoxedTest.java
單元測試
Primitive Type Streams API 是 Java 8 中增加的特性之一,允許通過提供聲明性 API 來方便且更有效地處理集合和其他元素序列。
intStreamBoxed
建立一個 IntStream,增加三個元素,使用 boxed 轉成 Object Stream ,內有三個元素。
@Test
public void intStreamBoxed() {
int expectedCount = 3;
IntStream stream = IntStream.of(1, 2, 3);
Stream<Integer> boxedStream = stream.boxed();
// boxedStream.forEach(System.out::println);
long count = boxedStream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
1
2
3
intStreamToList
建立一個 IntStream,增加三個元素,使用 boxed 、 collect 轉成 List ,內有三個元素。
@Test
public void intStreamToList() {
int expectedSize = 3;
IntStream stream = IntStream.of(1, 2, 3);
List<Integer> list = stream.boxed().collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
1
2
3
intStreamToArray
建立一個 IntStream,增加三個元素,使用 boxed 、 toArray 轉成 Array ,內有三個元素。
@Test
public void intStreamToArray() {
int expectedSize = 3;
IntStream stream = IntStream.of(1, 2, 3);
Integer[] array = stream.boxed().toArray(Integer[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[1, 2, 3]
longStreamBoxed
Boxed Streams in Java 建立一個 LongStream,增加三個元素,使用 boxed 轉成 Object Stream ,內有三個元素。
@Test
public void longStreamBoxed() {
int expectedCount = 3;
LongStream stream = LongStream.of(4, 5, 6);
Stream<Long> boxedStream = stream.boxed();
// boxedStream.forEach(System.out::println);
long count = boxedStream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
4
5
6
longStreamToList
Boxed Streams in Java 建立一個 LongStream,增加三個元素,使用 boxed 、 collect 轉成 List ,內有三個元素。
@Test
public void longStreamToList() {
int expectedSize = 3;
LongStream stream = LongStream.of(4, 5, 6);
List<Long> list = stream.boxed().collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
4
5
6
longStreamToArray
Boxed Streams in Java 建立一個 LongStream,增加三個元素,使用 boxed 、 toArray 轉成 Array ,內有三個元素。
@Test
public void longStreamToArray() {
int expectedSize = 3;
LongStream stream = LongStream.of(4, 5, 6);
Long[] array = stream.boxed().toArray(Long[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[4, 5, 6]
doubleStreamBoxed
Stream Boxed 建立一個 DoubleStream,增加三個元素,使用 boxed 轉成 Object Stream ,內有三個元素。
@Test
public void doubleStreamBoxed() {
int expectedCount = 3;
DoubleStream stream = DoubleStream.of(7.0, 8.0, 9.0);
Stream<Double> boxedStream = stream.boxed();
// boxedStream.forEach(System.out::println);
long count = boxedStream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
7.0
8.0
9.0
doubleStreamToList
Stream Boxed 建立一個 DoubleStream,增加三個元素,使用 boxed 、 collect 轉成 List ,內有三個元素。
@Test
public void doubleStreamToList() {
int expectedSize = 3;
DoubleStream stream = DoubleStream.of(7.0, 8.0, 9.0);
List<Double> list = stream.boxed().collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
7.0
8.0
9.0
doubleStreamToArray
Stream Boxed 建立一個 DoubleStream,增加三個元素,使用 boxed 、 toArray 轉成 Array ,內有三個元素。
@Test
public void doubleStreamToArray() {
int expectedSize = 3;
DoubleStream stream = DoubleStream.of(7.0, 8.0, 9.0);
Double[] array = stream.boxed().toArray(Double[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[7.0, 8.0, 9.0]
StreamBoxedTest.java
Primitive Type Streams 新增單元測試,驗證 Boxed Stream 是否符合預期。
package org.ruoxue.java_147.stream;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.junit.Test;
public class StreamBoxedTest {
public StreamBoxedTest() {
}
@Test
public void intStreamBoxed() {
int expectedCount = 3;
IntStream stream = IntStream.of(1, 2, 3);
Stream<Integer> boxedStream = stream.boxed();
// boxedStream.forEach(System.out::println);
long count = boxedStream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
@Test
public void intStreamToList() {
int expectedSize = 3;
IntStream stream = IntStream.of(1, 2, 3);
List<Integer> list = stream.boxed().collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
@Test
public void intStreamToArray() {
int expectedSize = 3;
IntStream stream = IntStream.of(1, 2, 3);
Integer[] array = stream.boxed().toArray(Integer[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
@Test
public void longStreamBoxed() {
int expectedCount = 3;
LongStream stream = LongStream.of(4, 5, 6);
Stream<Long> boxedStream = stream.boxed();
// boxedStream.forEach(System.out::println);
long count = boxedStream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
@Test
public void longStreamToList() {
int expectedSize = 3;
LongStream stream = LongStream.of(4, 5, 6);
List<Long> list = stream.boxed().collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
@Test
public void longStreamToArray() {
int expectedSize = 3;
LongStream stream = LongStream.of(4, 5, 6);
Long[] array = stream.boxed().toArray(Long[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
@Test
public void doubleStreamBoxed() {
int expectedCount = 3;
DoubleStream stream = DoubleStream.of(7.0, 8.0, 9.0);
Stream<Double> boxedStream = stream.boxed();
// boxedStream.forEach(System.out::println);
long count = boxedStream.peek(System.out::println).count();
assertEquals(expectedCount, count);
}
@Test
public void doubleStreamToList() {
int expectedSize = 3;
DoubleStream stream = DoubleStream.of(7.0, 8.0, 9.0);
List<Double> list = stream.boxed().collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
@Test
public void doubleStreamToArray() {
int expectedSize = 3;
DoubleStream stream = DoubleStream.of(7.0, 8.0, 9.0);
Double[] array = stream.boxed().toArray(Double[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
}
心得分享
Boxed Stream 提供了 3 種 基本型別 Stream ,轉成 Object Stream 、 List 、 Array 等。