How to Convert Stream to an Array

JavaJavaBeginner
Practice Now

Introduction

In Java, a stream is a sequence of elements on which we can perform operations in a functional approach. On the other hand, an array is an object that stores a similar type of element. In this lab, we will learn how to convert a stream to an array in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/FileandIOManagementGroup(["`File and I/O Management`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/generics("`Generics`") java/FileandIOManagementGroup -.-> java/stream("`Stream`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/generics -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/stream -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/classes_objects -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/class_methods -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/modifiers -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/oop -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/packages_api -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/identifier -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/arrays -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/comments -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/data_types -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/operators -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/output -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/strings -.-> lab-117425{{"`How to Convert Stream to an Array`"}} java/system_methods -.-> lab-117425{{"`How to Convert Stream to an Array`"}} end

Convert an Integer stream to an int Array

In this step, we will create a sequence of integers using the rangeClosed() method of IntStream and convert it to an array using the toArray() method.

import java.util.stream.IntStream;

public class StreamToArray {
    public static void main(String[] args){
        IntStream integerStream = IntStream.rangeClosed(1, 5);

        // convert the stream into an int array
        int[] arr = integerStream.toArray();

        // print each element of the array
        for(int e: arr) {
            System.out.println(e);
        }
    }
}

The output of the code will be as follows:

1
2
3
4
5

Convert a String Stream to a String Array

In this step, we will create a stream of string values from a list and convert it into an array using the toArray() method.

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

public class StreamToArray {
    public static void main(String[] args){
        Stream<String> stream = List.of("UK", "US", "BR").stream();

        // convert the string stream into a string array
        String[] strArray = stream.toArray(String[]::new);

        // print each element of the string array
        for(String str : strArray) {
            System.out.println(str);
        }

        // convert the string array into a stream again
        Stream<String> stringStream = Arrays.stream(strArray);

        // print each element of the stream
        stringStream.forEach(System.out::println);
    }
}

The output of the code will be as follows:

UK
US
BR

UK
US
BR

Summary

In this lab, we learned how to convert a stream to an array in Java. We used the toArray() method of the stream to convert it into an array. The syntax for this method is straightforward, and it returns an array of the specified type. We saw examples of converting integer and string streams into their respective arrays.

Other Java Tutorials you may like