Array to Stream in Java

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 + " ")); 
    }
}
Output:



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 + " "));
   }
}
Output:

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:

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 + " ")); 
    }
}
Output:

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 + " "));
   }
}
Output:

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:

Output:

3 5 6 8 9 
1.0 2.0 3.0 4.0 5.0 



In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.