In Java, you can make use of several ways to convert an array to a stream. Here are four ways to do it:
- Using Arrays.stream() method.
- Using Stream.of() method.
- Using Arrays.asList() method.
- Using Stream.Builder() method.
Let's take a look at each of them one by one,
1. Using Arrays.stream() method.
import java.util.Arrays;
import java.util.stream.Stream;
public class ArrayToSteamExample1 {
public static void main(String[] args) {
//Our String Array
String[] countryArray = {"China", "France", "USA", "Canada","Sweden"};
//String array converted to Stream using Arrays.stream()
Stream<String> countryStream = Arrays.stream(countryArray);
}
}
2. Using Stream.of() method
import java.util.stream.Stream;
public class ArrayToSteamExample2 {
public static void main(String[] args) {
//Our String Array
String[] countryArray = {"China", "France", "USA", "Canada","Sweden"};
//String array converted to Stream using Arrays.stream()
Stream<String> countryStream = Stream.of(countryArray);
}
}
3. Using Arrays.asList() method
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class ArrayToSteamExample3 {
public static void main(String[] args) {
//Our String Array
String[] countryArray = {"China", "France", "USA", "Canada","Sweden"};
//Step 1: Convert Array to List
List<String> countryList = Arrays.asList(countryArray);
//Step 2: Convert List to Steam
Stream<String> countryStream = countryList.stream();
}
}
4. Using Stream.Builder() method
import java.util.stream.Stream;
public class ArrayToSteamExample4 {
public static void main(String[] args) {
//Our String Array
String[] countryArray = {"China", "France", "USA", "Canada","Sweden"};
//Create a Stream Builder object
Stream.Builder<String> builder = Stream.builder();
for (String country : countryArray) {
builder.add(country);
}
Stream<String> countryStream = builder.build();
}
}
In the above example the Stream class in Java provides a Builder() method that can be used to create a stream of elements.
Example:

China
France
USA
Canada
Sweden
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Find Nearest Gas Station using Google Map App on your Phone - Google
- [fix] Docker: OCI runtime exec failed unable to start container process - Docker
- How to hide or disable iOS 14 App Library on iPhone? - Apple
- How to Copy Entire Directory to another Directory in Linux - Linux
- Spring Boot: Transactions Management with JDBCTemplate Example - Java
- Difference between Sublime Text vs Visual Studio Code (VS Code) - Sublime-Text
- [macOS] Change homepage Macbook Safari Browser - MacOS
- How to take a screenshot on a Mac - updated for Ventura 13 [updated 2023] - MacOS