GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Array to Stream in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Array to Stream in Java

Last Updated : 17 Jul, 2026

Arrays are one of the most commonly used data structures in Java. With the introduction of the Stream API in Java 8, arrays can be easily converted into streams, allowing developers to perform filtering, mapping, sorting, and other operations in a functional and concise way.

  • Converts arrays into streams for functional-style data processing.
  • Supports both object arrays and primitive arrays.
  • Enables powerful operations such as filter(), map(), sorted(), reduce(), and collect().

Methods to Convert an Array to a Stream

Java provides multiple ways to convert an array into a stream depending on the array type.

1. Convert an Array to a Stream using Arrays.stream()

The Arrays.stream() method is the most commonly used way to create a stream from an array. It works with both object arrays and primitive arrays.

Syntax:

Arrays.stream(array);

Example using Arrays.stream() to convert string array to stream.

The Arrays.stream() method creates a stream from a String array, and each element is displayed using the forEach() terminal operation.

Java
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args)
    {
        // Converting String array to stream
        String[] arr = { "Geeks", "for", "Geeks" };

        // Using Arrays.stream to convert an
        // array into Stream
        Stream<String> stm = Arrays.stream(arr);

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

Output
Geeks for Geeks 

Example Using Arrays.stream() to convert long and double arrays to stream.

Arrays.stream() converts long and double arrays into their respective primitive streams, allowing each element to be processed efficiently.

Java
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args)
    {        // Converting long array to stream
        long[] arrL = { 3L, 5L, 6L, 8L, 9L };
        LongStream stmL = Arrays.stream(arrL);
        stmL.forEach(number -> System.out.print(number + " "));

        System.out.println();

        // Converting double array to stream
        double[] arrD = { 1, 2, 3, 4, 5 };
        DoubleStream stmD = Arrays.stream(arrD);
        stmD.forEach(d -> System.out.print(d + " "));
    }
}

Output:


Output
3 5 6 8 9 
1.0 2.0 3.0 4.0 5.0 

2. Convert an Array to a Stream using Stream.of()

The Stream.of() method creates a stream from an object array. It is suitable for arrays of reference types such as String, Integer, or custom objects.

Syntax:

Stream.of(array);

Note : For object arrays, Stream.of() internally uses Arrays.stream().

Example using Stream.of( ) to convert string array to stream.

Stream.of() creates a stream from a String array, and the stream elements are processed one by one using forEach().

Java
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args)
    {
        // Converting String array to stream
        String[] arr = { "Geeks", "for", "Geeks" };

        // Using Arrays.stream to convert an
        // array into Stream
        Stream<String> stm = Stream.of(arr);

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

Output
Geeks for Geeks 

Example using Stream.of( ) to convert long and double arrays to stream.

LongStream.of() and DoubleStream.of() create streams directly from primitive arrays, which are then traversed and printed

Java
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args)
    {        // Converting long array to stream
        long[] arrL = { 3L, 5L, 6L, 8L, 9L };
        LongStream stmL = LongStream.of(arrL);
        stmL.forEach(number -> System.out.print(number + " "));

        System.out.println();

        // Converting double array to stream
        double[] arrD = { 1, 2, 3, 4, 5 };
        DoubleStream stmD = DoubleStream.of(arrD);
        stmD.forEach(d -> System.out.print(d + " "));
    }
}

Output
3 5 6 8 9 
1.0 2.0 3.0 4.0 5.0 

3. Convert an Array to a Stream using Arrays.spliterator()

A Spliterator is an object used to traverse and partition elements efficiently. It can be converted into a stream using StreamSupport.stream().

Syntax:

StreamSupport.stream(
Arrays.spliterator(array), false
);

Example using Arrays.spliterator() to Convert a String Array to a Stream

A spliterator is created from the array and then converted into a sequential stream using StreamSupport.stream().

Java
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args)
    {
        // Converting String array to Spliterator
        String[] arr = { "Geeks", "for", "Geeks" };

        // Creating stream using Spliterator
        Stream<String> stm = StreamSupport.stream(
                Arrays.spliterator(arr), false);

        // Displaying stream elements
        stm.forEach(str -> System.out.print(str + " "));
    }
}

Output
Geeks for Geeks 

Example using Arrays.spliterator() to Convert an Integer Array to a Stream

The array is first converted into a Spliterator, which is then used to create a stream for processing its elements.

Java
import java.util.*;
import java.util.stream.*;

class GFG {

    public static void main(String[] args)
    {
        Integer[] arr = { 10, 20, 30, 40, 50 };

        Stream<Integer> stm = StreamSupport.stream(
                Arrays.spliterator(arr), false);

        stm.forEach(num -> System.out.print(num + " "));
    }
}

Output
10 20 30 40 50 

Common Operations After Converting an Array to a Stream

After converting an array into a stream, you can perform various intermediate and terminal operations to process its elements efficiently. These operations allow you to filter, transform, sort, aggregate, and collect data in a concise and functional manner.

  • filter(): Selects elements that satisfy a given condition.
  • map(): Transforms each element into another form.
  • sorted(): Arranges elements in their natural or custom order.
  • distinct(): Removes duplicate elements from the stream.
  • limit(): Restricts the stream to a specified number of elements.
  • skip(): Skips the specified number of elements.
  • reduce(): Combines stream elements into a single result.
  • collect(): Collects stream elements into a collection or another data structure.
  • forEach(): Performs an action on each stream element.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.