Convert Array to Stream Using Java 8

Harinath
867 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Object and Primitive Arrays

This Java template lets you get started quickly with a simple one-page playground.

1
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// {...}
//Object Array
String[] array = {"t", "e", "c", "h", "i", "o"};
//Arrays.stream
Stream<String> stream1 = Arrays.stream(array);
stream1.forEach(x -> System.out.println(x));
//Stream.of
Stream<String> stream2 = Stream.of(array);
stream2.forEach(x -> System.out.println(x));
//Premitive Array
int[] intArray = {1, 2, 3, 4, 5};
//Arrays.stream
IntStream intStream1 = Arrays.stream(intArray);
intStream1.forEach(x -> System.out.println(x));
//Stream.of
Stream<int[]> tempStream = Stream.of(intArray);
IntStream intStream2 = tempStream.flatMapToInt(x -> Arrays.stream(x));
intStream2.forEach(x -> System.out.println(x));
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Advanced usage

If you want a more complex example (external libraries, viewers...), use the Advanced Java template

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content