🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java 8, the Stream API provides an easy way to process data in a functional style. Converting an array to a stream allows you to use powerful stream operations such as filter(), map(), and collect(). Java provides two main methods for converting arrays into streams: Arrays.stream() and Stream.of().
In this guide, we will learn how to convert an array into a stream using Java 8.
Solution Steps
- Define the Array: Create an array of elements (e.g., strings or integers) that you want to convert to a stream.
- Convert Array to Stream Using
Arrays.stream(): Use theArrays.stream()method to convert the array to a stream. - Convert Array to Stream Using
Stream.of(): Alternatively, useStream.of()to convert the array to a stream. - Process the Stream: Apply operations like
forEach(),filter(), ormap()to process the stream. - Display the Result: Print or collect the processed elements.
Java Program
Example 1: Using Arrays.stream()
import java.util.Arrays;
import java.util.stream.Stream;
public class ArrayToStreamUsingArraysStream {
public static void main(String[] args) {
// Step 1: Define the array of strings
String[] fruits = {"Apple", "Banana", "Orange", "Mango"};
// Step 2: Convert the array to a stream using Arrays.stream()
Stream<String> fruitStream = Arrays.stream(fruits);
// Step 3: Process and display each element in the stream
fruitStream.forEach(System.out::println);
}
}
Output
Apple
Banana
Orange
Mango
Explanation
Step 1: Define the Array
We define an array of strings:
String[] fruits = {"Apple", "Banana", "Orange", "Mango"};
Step 2: Convert the Array to a Stream
We use the Arrays.stream() method to convert the array to a stream:
Stream<String> fruitStream = Arrays.stream(fruits);
Step 3: Process the Stream
We use forEach() to print each element of the stream:
fruitStream.forEach(System.out::println);
Example 2: Using Stream.of()
import java.util.stream.Stream;
public class ArrayToStreamUsingStreamOf {
public static void main(String[] args) {
// Step 1: Define the array of integers
Integer[] numbers = {1, 2, 3, 4, 5};
// Step 2: Convert the array to a stream using Stream.of()
Stream<Integer> numberStream = Stream.of(numbers);
// Step 3: Process and display each element in the stream
numberStream.forEach(System.out::println);
}
}
Output
1
2
3
4
5
Explanation
Step 1: Define the Array
We define an array of integers:
Integer[] numbers = {1, 2, 3, 4, 5};
Step 2: Convert the Array to a Stream
We use Stream.of() to convert the array into a stream:
Stream<Integer> numberStream = Stream.of(numbers);
Step 3: Process the Stream
We use forEach() to print each element in the stream:
numberStream.forEach(System.out::println);
Example 3: Processing the Stream (Filtering Even Numbers)
You can also apply operations like filter() to process elements in the stream.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayToStreamWithProcessing {
public static void main(String[] args) {
// Step 1: Define an array of integers
Integer[] numbers = {10, 15, 20, 25, 30, 35};
// Step 2: Convert the array to a stream and filter even numbers
List<Integer> evenNumbers = Arrays.stream(numbers)
.filter(n -> n % 2 == 0) // Filter even numbers
.collect(Collectors.toList()); // Collect to List
// Step 3: Display the filtered list of even numbers
System.out.println(evenNumbers);
}
}
Output
[10, 20, 30]
Explanation
Step 1: Define the Array
We define an array of integers:
Integer[] numbers = {10, 15, 20, 25, 30, 35};
Step 2: Convert the Array to a Stream and Filter Even Numbers
We convert the array into a stream using Arrays.stream(). The filter() method is used to retain only even numbers, and the collect() method gathers the results into a list:
List<Integer> evenNumbers = Arrays.stream(numbers)
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
Step 3: Display the Result
The filtered list of even numbers is printed:
System.out.println(evenNumbers);
Conclusion
In Java 8, converting an array to a stream is simple and can be done using either Arrays.stream() or Stream.of(). Once converted to a stream, you can easily process the elements using stream operations such as filter(), map(), and collect(). This makes working with arrays more functional and flexible.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment