Open In App

Program to Convert Stream to an Array in Java

Improve
Improve
Like Article
Like
Save
Share
Report

A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result.

An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.

Below are various methods to convert Stream into an Array:

  1. Using toArray(): Stream provides toArray() method that returns an array containing the elements of the stream in the form of Object array.

    Syntax:

    stream.toArray()

    Algorithm:

    1. Get the Stream
    2. Convert Stream into an Array using Stream.toArray() method.
    3. The obtained array is of type Object[]
    4. Return the Array Object[]

    Program:




    // Java Program to convert
    // Stream to array in Java 8
      
    import java.util.*;
    import java.util.stream.*;
    import java.util.function.Function;
      
    class GFG {
      
        // Function to convert Stream to Array
        public static <T> Object[] convertStreamToArray(Stream<T> stream)
        {
            return stream.toArray();
        }
      
        public static void main(String args[])
        {
            // Create a stream of integers
            Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
      
            // Convert Stream to array
            Object[] array = convertStreamToArray(stream);
      
            // Print the array of stream
            System.out.println("Array from Stream: "
                               + Arrays.toString(array));
        }
    }

    
    

    Output:

    Array from Stream: [1, 2, 3, 4, 5]
    
  2. Using toArray(IntFunction generator): This method returns an array containing the elements of this stream, using the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.

    Syntax:

    <A> A[] toArray(IntFunction<A[]> generator)

    Algorithm:

    1. Get the Stream
    2. Convert Stream into an Array using Stream.toArray() method by passing Object[]::new as the generator function to allocate the returned array.
    3. The obtained array is of type Object[]
    4. Return the Array Object[]

    Program:




    // Java Program to convert
    // Stream to array in Java 8
      
    import java.util.*;
    import java.util.stream.*;
    import java.util.function.Function;
      
    class GFG {
      
        // Function to convert Stream to Array
        public static <T> Object[] convertStreamToArray(Stream<T> stream)
        {
            return stream.toArray(Object[] ::new);
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
      
            // Convert Stream to array
            Object[] array = convertStreamToArray(stream);
      
            // Print the array of stream
            System.out.println("Array from Stream: "
                               + Arrays.toString(array));
        }
    }

    
    

    Output:

    Array from Stream: [1, 2, 3, 4, 5]
    
  3. Stream to int[] using mapToInt(): Java 8 Stream API provides mapToInt() method that returns an IntStream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. The obtained IntStream is then converted into int[] using toArray().

    Algorithm:

    1. Get the Stream of Integers
    2. Convert Stream into an IntStream using Stream.mapToInt() method.
    3. Convert the obtained IntStream into int[] using toArray()
    4. The obtained array is of type Integer
    5. Return the Array int[]

    Program:




    // Java Program to convert
    // Stream to array in Java 8
      
    import java.util.*;
    import java.util.stream.*;
    import java.util.function.Function;
      
    class GFG {
      
        // Function to convert Stream to Array
        public static int[] convertStreamToArray(Stream<Integer> stream)
        {
            return stream.mapToInt(Integer::intValue).toArray();
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
      
            // Convert Stream to array
            int[] array = convertStreamToArray(stream);
      
            // Print the array of stream
            System.out.println("Array of Integer from Stream: "
                               + Arrays.toString(array));
        }
    }

    
    

    Output:

    Array of Integer from Stream: [1, 2, 3, 4, 5]
    


Last Updated : 11 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads