Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Arrays Stream() Method in Java

Last Updated on June 23, 2023 by Prepbytes

In this tutorial, we will delve into the stream() method of the Arrays class in Java. This particular method retrieves a stream of data of a specified type from an array. It is capable of handling various data types such as double, int, long, and generic classes. We will examine the syntax and provide examples to demonstrate the usage of this method.

What is Java Arrays Stream() Method?

The stream() method of the Arrays class in Java is a utility method that allows you to obtain a sequential stream of elements from an array. It was introduced in Java 8 as part of the Stream API.

The stream() method is overloaded for different types of arrays, such as arrays of primitive types (int[], double[], etc.) and arrays of reference types (Object[]). When invoked on an array, it returns a sequential stream that represents the elements of the array as individual stream elements.

Syntax of Array Stream() in Java

Here’s the general syntax of the Array stream() method:

static datatypeStream stream(datatype[] array)

Here, the array represents the array from which you want to obtain the stream. The method returns a Stream object that you can use to perform various operations on the array elements, such as filtering, mapping, or reducing.

The stream() method is commonly used in combination with other stream operations to process arrays in a functional programming style, enabling concise and expressive code for array manipulation and processing.

Example of Array Stream() in Java

In this example, we demonstrate the conversion of an array, arr, into a stream of strings using the stream() method. Subsequently, we utilize the forEach method to print each element of the stream

import java.util.Arrays;
import java.util.stream.Stream;
class StudyTonight{ 
    public static void main(String args[]) 
    {  
        String[] array = { "java", "cpp", "c", "python" }; 
        Stream<String> myStream = Arrays.stream(array); 
        myStream.forEach(str -> System.out.print(str + " ")); 
    }
}

Output:

java cpp c python

Example to Convert int array to Stream

In the given example, we convert an array of integers to a stream using the stream() method. To hold the resulting stream, we employ the dedicated class IntStream, designed specifically for streams of integers. Finally, we utilize the forEach method to print each element of the converted stream of integers.

import java.util.Arrays;
import java.util.stream.IntStream;
class StudyTonight{ 
    public static void main(String args[]) 
    {  
        int[] array = {12, 41, 18, 4, 5, 31}; 
        IntStream  myStream = Arrays.stream(array); 
        myStream.forEach(str -> System.out.print(str + " ")); 		
    }
}



Output:

12 41 18 4 5 31

Conclusion
The stream() method of the Arrays class in Java provides a convenient way to convert arrays into streams. It allows you to perform various stream operations on array elements, such as filtering, mapping, or reducing, in a functional programming style. The stream() method is overloaded to handle different types of arrays, including arrays of primitive types and reference types. By leveraging the power of streams, you can write concise and expressive code for array manipulation and processing.

FAQs related to the Arrays stream method in Java

Q1. What is the advantage of using the stream() method for arrays?
The stream() method allows you to leverage the powerful Stream API to perform various operations on array elements. It enables functional programming paradigms, such as filtering, mapping, and reducing, resulting in concise and expressive code.

Q2. Can I use the stream() method on multidimensional arrays?
No, the stream() method is not directly applicable to multidimensional arrays. However, you can flatten a multidimensional array into a one-dimensional array and then use the stream() method on it.

Q3. What is the difference between a sequential stream and a parallel stream?
The stream() method returns a sequential stream by default. A sequential stream processes elements in a single thread, while a parallel stream can leverage multiple threads for concurrent processing, potentially improving performance for large datasets.

Q4. How can I convert a stream back to an array?
You can use the toArray() method on a stream to convert it back to an array. For example, stream.toArray() will return an array containing the elements of the stream.

Q5. Are streams in Java mutable or immutable?
Streams in Java are generally immutable, meaning they cannot be modified once created. However, you can apply various stream operations to transform or filter the elements and obtain a new stream as a result.

Q6. Can I use the stream() method on an empty array?
Yes, the stream() method can be used on an empty array. It will return an empty stream, allowing you to perform stream operations or collect the elements as needed.

Leave a Reply

Your email address will not be published. Required fields are marked *