Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Arrays stream() Method

In this tutorial, we will explore about stream() method of the Arrays class in Java. This method returns a stream of the specified data type from an array. It supports the various datatypes like double, int, long, and generic classes. Let's see the syntax and examples of this method.

Syntax

static datatypeStream stream(datatype[] array)

List of Overloading Methods of stream() Method

This table contains all the overloaded variants of stream() method.

Method Description

static DoubleStream stream(double[] array)

This method returns a sequential DoubleStream with the specified array as its source.

static DoubleStream stream(double[] array, int startInclusive, int endExclusive)

This method returns a sequential DoubleStream with the specified range of the specified array as its source.

static IntStream stream(int[] array)

This method returns a sequential IntStream with the specified array as its source.

static IntStream stream(int[] array, int startInclusive, int endExclusive)

This method returns a sequential IntStream with the specified range of the specified array as its source.

static LongStream stream(long[] array)

This method returns a sequential LongStream with the specified array as its source.

static LongStream stream(long[] array, int startInclusive, int endExclusive)

This method returns a sequential LongStream with the specified range of the specified array as its source.

static <T> Stream<T> stream(T[] array)

This method returns a sequential Stream with the specified array as its source.

static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive)

This method returns a sequential Stream with the specified range of the specified array as its source.

Example of stream() method

In the following example, we are converting an array arr to a stream of strings using stream() method. After getting a stream of string we are printing it using forEach method.

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


java cpp c python

Example to Convert int array to Stream

In the following example, we are converting an array of int to stream using stream() method, and to store that stream we have a special class i.e. IntStream which takes about storing a stream of integers. Finally, that converted stream of int array we print with the help of the forEach method.

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


12 41 18 4 5 31

Example of stream() with a range of array

Both the examples given above were similar because they are just converting a given array to a stream of a given datatype. What if we want to convert specific elements to stream? For that, we are given a special overloading method where we pass startInclusive and endExclusive variables to define the range of array and elements from this specific range only will be converted to stream.

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


java cpp

Example with all overloading stream() Methods

In the example given below, we have mentioned all the overloading methods the stream() together to understand it better. In this example, all the methods get called with range and without range

import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
class StudyTonight{ 
	public static void main(String args[]) 
	{  
		//Example: static DoubleStream stream(double[] array)
		double array1[] = {2.2, 3.3, 4.4, 5.5, 6.6};
		DoubleStream doubleStream = Arrays.stream(array1);
		doubleStream.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

		//Example: static DoubleStream stream(double[] array, int startInclusive, int endExclusive)
		doubleStream = Arrays.stream(array1, 1, 3);
		doubleStream.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

		//Example: static IntStream	stream(int[] array)
		int array2[] = {4, 12, 7, 8, 5, 1, 3, 21, 62};
		IntStream intStream = Arrays.stream(array2);
		intStream.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

		//Example: static IntStream	stream(int[] array, int startInclusive, int endExclusive)
		intStream = Arrays.stream(array2,3,7);
		intStream.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

		//Example: static LongStream stream(long[] array)
		long array3[]= {10001, 10002, 10003, 10004, 10005, 10006};
		LongStream longStream =Arrays.stream(array3);
		longStream.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

		//Example:static LongStream	stream(long[] array, int startInclusive, int endExclusive)
		longStream =Arrays.stream(array3, 2, 5);
		longStream.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

		//Example: static <T> Stream<T>	stream(T[] array)
		String[] array4 = {"Welcome", "to", "study", "tonight"}; 
		Stream<String> streamString = Arrays.stream(array4); 
		streamString.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

		//Example:static <T> Stream<T>	stream(T[] array, int startInclusive, int endExclusive)
		streamString = Arrays.stream(array4, 0, 2); 
		streamString.forEach(val->System.out.print(val+" "));
		System.out.print("\n");

	}
}


2.2 3.3 4.4 5.5 6.6
3.3 4.4
4 12 7 8 5 1 3 21 62
8 5 1 3
10001 10002 10003 10004 10005 10006
10003 10004 10005
Welcome to study tonight
Welcome to

Conclusion:

In this tutorial, we learned how to use strem() method. This method returns a sequential stream of the specified data type. We are also given a special overloading method where we pass startInclusive and endExclusive variables to define the range of array and elements from this specific range only will be converted to stream.