Open In App

Arrays.stream() Method in Java

Last Updated : 16 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Arrays.stream() method is used to get a sequential Stream from the elements of an array passed as a parameter. Below is a simple example that uses Arrays.stream() to convert a String array into a Stream for sequential processing.

Example:

// Java program to demonstrate Arrays.stream() method
import java.util.*;
import java.util.stream.*;

class GFG {
    public static void main(String[] args) {

        // create a String array
        String[] arr = { "Geeks", "for", "Geeks" };

        // use Arrays.stream() to convert array into Stream
        Stream<String> st = Arrays.stream(arr);

        st.forEach(s -> System.out.print(s + " "));
    }
}

Output
Geeks for Geeks 

Syntax of Arrays.stream() Method

1. To Convert Entire Array

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

Parameters:

  • array: This is the array, whose elements are to be converted into a sequential stream. It is a mandatory parameter of type T[] (an array of any object type).

Return Type: This method returns a Stream<T>, which is a sequential stream of the elements of the provided array.

2. To Convert a Range within an Array

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

Parameters:

  • array: The array to be partially converted to a stream.
  • startInclusive: The starting index of the range to include in the stream (inclusive).
  • endExclusive: The end index of the range (exclusive).

Return Type: This returns a Stream<T>, which is a sequential stream that contains elements from the specified range in the array.

Example 1: Convert an int Array to an IntStream

In this example, we create an IntStream from a full int array and print each element in sequence.

// Java program to Convert an 
// int Array to an IntStream
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args) {
        
        // create an integer array
        int arr[] = { 1, 2, 3, 4,};

        // use Arrays.stream() to convert
        // array into Stream
        IntStream st = Arrays.stream(arr);

        st.forEach(s -> System.out.print(s + " "));
    }
}

Output
1 2 3 4 

Example 2: Convert a Specific Range of a String Array to a Stream

In this example, we will generate a Stream from a specific range within a String array and then will print the elements within the selected range.

// Java program to Convert a Specific Range
// of a String Array to a Stream
import java.util.*;
import java.util.stream.*;

class GFG {
    public static void main(String[] args) {

        // create a String array
        String[] arr = { "Geeks", "for", "Geeks", };

        // use Arrays.stream() to convert
        // array into Stream
        Stream<String> st = Arrays.stream(arr, 1, 2);

        st.forEach(s -> System.out.print(s + " "));
    }
}

Output
for 

Example 3: Convert a Specific Range of an int Array to an IntStream

In this example, we will create an IntStream from a range of elements in an int array and then we will print only those elements within the specified range.

// Java program to Convert a Specific Range
// of an int Array to an IntStream
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args) {
        
        // create an integer array
        int arr[] = { 1, 2, 3, 4,};

        // use Arrays.stream() to convert
        // array into Stream
        IntStream st = Arrays.stream(arr, 1, 3);

        // Displaying elements in Stream
        st.forEach(s -> System.out.print(s + " "));
    }
}

Output
2 3 


Practice Tags :

Similar Reads

three90RightbarBannerImg