Java 8 Convert a Stream to Array
By:Roy.LiuLast updated:2019-08-11
In Java 8, we can use .toArray() to convert a Stream into an Array.
1. Stream -> String[]
StreamString.java
package com.mkyong; import java.util.Arrays; public class StreamString { public static void main(String[] args) { String lines = "I Love Java 8 Stream!"; // split by space, uppercase, and convert to Array String[] result = Arrays.stream(lines.split("\\s+")) .map(String::toUpperCase) .toArray(String[]::new); for (String s : result) { System.out.println(s);
Output
LOVE JAVA STREAM!
2. IntStream -> Integer[] or int[]
2.1 Stream to Integer[]
StreamInt1.java
package com.mkyong; import java.util.Arrays; public class StreamInt1 { public static void main(String[] args) { int[] num = {1, 2, 3, 4, 5}; Integer[] result = Arrays.stream(num) .map(x -> x * 2) .boxed() .toArray(Integer[]::new); System.out.println(Arrays.asList(result));
Output
[2, 4, 6, 8, 10]
2.2 Stream to int[]
StreamInt2.java
package com.mkyong; import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.Stream; public class StreamInt2 { public static void main(String[] args) { // IntStream -> int[] int[] stream1 = IntStream.rangeClosed(1, 5).toArray(); System.out.println(Arrays.toString(stream1)); // Stream<Integer> -> int[] Stream<Integer> stream2 = Stream.of(1, 2, 3, 4, 5); int[] result = stream2.mapToInt(x -> x).toArray(); System.out.println(Arrays.toString(result));
Output
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
From:一号门
COMMENTS