Program to convert Boxed Array to Stream in Java

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.

A boxed array is an array which is defined in the form of an object, instead of the primitives.
Example: int a = 4;

Examples:



Input: Array: [Geeks, forGeeks, A computer Portal]
Output: Stream: [Geeks, forGeeks, A computer Portal]

Input: Array: [1, 2, 3, 4, 5]
Output: Stream: [1, 2, 3, 4, 5]

Below are methods to convert Boxed Array to Stream in Java:

  1. Using Arrays.stream():

    Algorithm:

    1. Get the Array to be converted.
    2. Convert the array into Stream using Arrays.stream() method by passing the array as the parameter.
    3. Return the formed Stream

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to Stream
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert 
        // an Array to Stream
        public static <T> Stream<T> 
                    convertArrayToStream(T array[])
        {
      
            // Return the converted Stream
            return Arrays.stream(array);
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks"
                                        "A Computer Portal" };
      
            // Print the Array
            System.out.println("Array: "
                                    + Arrays.toString(array));
      
            // convert the Array to Stream
            Stream<String>
                stream = convertArrayToStream(array);
      
            // Print the Stream
            System.out.println("Stream: " 
                        + Arrays.toString(stream.toArray()));
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    Stream: [Geeks, forGeeks, A computer Portal]
    
  2. Using Stream.of(): Stream.of() method creates a Stream directly with the values or collection passed as the parameter.

    Algorithm:

    1. Get the Array to be converted.
    2. Convert the array into Stream using Stream.of() method by passing the array as the parameter.
    3. Return the formed Stream

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to Stream
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert
        // an Array to Stream
        public static <T> Stream<T> 
                convertArrayToStream(T array[])
        {
      
            // Return the converted Stream
            return Stream.of(array);
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks"
                                        "A Computer Portal" };
      
            // Print the Array
            System.out.println("Array: "
                                + Arrays.toString(array));
      
            // convert the Array to Stream
            Stream<String>
                stream = convertArrayToStream(array);
      
            // Print the Stream
            System.out.println("Stream: " 
                            + Arrays.toString(stream.toArray()));
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    Stream: [Geeks, forGeeks, A computer Portal]
    
  3. Using List.stream(): This is an indirect method in which the array is first converted into a List using Arrays.asList() method. Then the formed list is converted into a Stream using List.stream() method.

    Algorithm:

    1. Get the Array to be converted.
    2. Convert the array into List using Arrays.asList() method by passing the array as the parameter.
    3. Convert the formed List into Stream using List.stream() method.
    4. Return the formed Stream

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to Stream
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert 
        // an Array to Stream
        public static <T> Stream<T> 
                    convertArrayToStream(T array[])
        {
      
            // Return the converted Stream
            return Arrays
                .asList(array)
                .stream();
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks"
                                        "A Computer Portal" };
      
            // Print the Array
            System.out.println("Array: " 
                            + Arrays.toString(array));
      
            // convert the Array to Stream
            Stream<String>
                stream = convertArrayToStream(array);
      
            // Print the Stream
            System.out.println("Stream: " 
                       + Arrays.toString(stream.toArray()));
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    Stream: [Geeks, forGeeks, A computer Portal]
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.