Array to Stream in Java - GeeksforGeeks

11 Dec, 2018
Prerequisite : Stream In Java
Using Arrays.stream() :
Syntax :
public static <T> Stream<T> getStream(T[] arr)
{
  return Arrays.stream(arr);
}

where, T represents generic type.

Example 1 : Arrays.stream() to convert string array to stream.
// Java code for converting string array// to stream using Arrays.stream()import java.util.*;import java.util.stream.*;  class GFG {      public static void main(String[] args)    {        // Converting String array to stream        String[] arr = { "Geeks", "for", "Geeks" };          // Using Arrays.stream to convert an        // array into Stream        Stream<String> stm = Arrays.stream(arr);          // Displaying elements in Stream        stm.forEach(str -> System.out.print(str + " "));     }}
Geeks for Geeks 

Example 2 : Arrays.stream() to convert int array to stream.
// Java code for converting string array// to stream using Arrays.stream()import java.util.*;import java.util.stream.*;  class GFG {      public static void main(String[] args)    {        // Converting int array to stream        int arr[] = { 1, 2, 3, 4, 5 };          IntStream stm = Arrays.stream(arr);          stm.forEach(a -> System.out.print(a + " "));   }}
1 2 3 4 5 

Example 3 : Arrays.stream() to convert long and double arrays to stream.
// Java code for converting string array// to stream using Arrays.stream()import java.util.*;import java.util.stream.*;  class GFG {      public static void main(String[] args)    {        // Converting long array to stream        long[] arrL = { 3L, 5L, 6L, 8L, 9L };        LongStream stmL = Arrays.stream(arrL);        stmL.forEach(number -> System.out.print(number + " "));          System.out.println();          // Converting double array to stream        double[] arrD = { 1, 2, 3, 4, 5 };        DoubleStream stmD = Arrays.stream(arrD);        stmD.forEach(d -> System.out.print(d + " "));    }}
Output:
3 5 6 8 9 
1.0 2.0 3.0 4.0 5.0 

Using Stream.of(), IntStream.of(), LongStream.of() & DoubleStream.of() :
Syntax :
public static <T> Stream<T> getStream(T[] arr)
{
  return Stream.of(arr);
}

where, T represents generic type.

Syntax of other functions is similar

Note : For object arrays, Stream.of() internally uses Arrays.stream().
Example 1 : Arrays.stream() to convert string array to stream.
// Java code for converting string array// to stream using Stream.of()import java.util.*;import java.util.stream.*;  class GFG {      public static void main(String[] args)    {        // Converting String array to stream        String[] arr = { "Geeks", "for", "Geeks" };          // Using Arrays.stream to convert an        // array into Stream        Stream<String> stm = Stream.of(arr);          // Displaying elements in Stream        stm.forEach(str -> System.out.print(str + " "));     }}
Geeks for Geeks 

Example 2 : Arrays.stream() to convert int array to stream.
// Java code for converting string array// to stream using IntStream.of()import java.util.*;import java.util.stream.*;  class GFG {      public static void main(String[] args)    {        // Converting int array to stream        int arr[] = { 1, 2, 3, 4, 5 };          IntStream stm = IntStream.of(arr);          stm.forEach(a -> System.out.print(a + " "));   }}
1 2 3 4 5 

Example 3 : Arrays.stream() to convert long and double arrays to stream.
// Java code for converting string array// to stream using  DoubleStream.of()import java.util.*;import java.util.stream.*;  class GFG {      public static void main(String[] args)    {        // Converting long array to stream        long[] arrL = { 3L, 5L, 6L, 8L, 9L };        LongStream stmL = LongStream.of(arrL);        stmL.forEach(number -> System.out.print(number + " "));          System.out.println();          // Converting double array to stream        double[] arrD = { 1, 2, 3, 4, 5 };        DoubleStream stmD = DoubleStream.of(arrD);        stmD.forEach(d -> System.out.print(d + " "));    }}
Output:
3 5 6 8 9 
1.0 2.0 3.0 4.0 5.0 

Java - util package
Java-Array-Programs
Java-Arrays
java-stream
Java-Stream-programs
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Generics in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Introduction to Java
Difference between Abstract Class and Interface in Java