Java Stream How to - Create Stream from array








Question

We would like to know how to create Stream from array.

Answer

import java.util.Arrays;
import java.util.stream.Stream;
//  w ww  .ja v  a2  s. c o m
public class Main {
   public static void main(String[] args) {
     Integer[] numbers = {1, 2, 3, 4};
     Stream<Integer> arrayStreams1 = Stream.of(numbers);

     System.out.println(Arrays.toString(arrayStreams1.toArray()));
   }

}

The code above generates the following result.