Java - Streams from Arrays

Introduction

Arrays class stream() static method creates sequential streams from arrays.

It can create an IntStream from an int array, a LongStream from a long array, a DoubleStream from a double array, and a Stream<T> from an array of the reference type T.

The following code creates an IntStream and a Stream<String> from an int array and a String array:

Demo

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {

    // Creates a stream from an int array with elements 1, 2, and 3
    IntStream numbers = Arrays.stream(new int[] { 1, 2, 3 });
    numbers.forEach(System.out::println);
    // Creates a stream from a String array with elements "Java", and "Jeff"
    Stream<String> names = Arrays.stream(new String[] { "Java", "Jeff" });
    names.forEach(System.out::println);
  }//from ww  w . j  a  va 2s .c  om
}

Result