Back
Close

Convert Array to Stream Using Java 8

Harinath
3,440 views

Object and Primitive Arrays

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

// {...}
//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

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants