🎓 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
In this guide, you will learn about the Stream toArray() method in Java programming and how to use it with an example.
1. Stream toArray() Method Overview
Definition:
The Stream.toArray() method is a terminal operation used to obtain an array containing the elements of the stream.
Syntax:
Object[] toArray()
<A> A[] toArray(IntFunction<A[]> generator)
Parameters:
- generator: a function that produces a new array of the desired type and the provided length.
Key Points:
- It is a terminal operation, which means it ends the stream pipeline.
- The toArray() method comes in two variants:
1. One that produces an Object[].
2. Another that produces an array of a type determined by the provided generator function.
- If the stream is infinite, a java.lang.OutOfMemoryError may be thrown when trying to allocate an array of infinite length.
- The generator function gives more control over the runtime type of the output array.
2. Stream toArray() Method Example
import java.util.stream.Stream;
public class StreamToArrayExample {
public static void main(String[] args) {
Stream<String> fruitStream = Stream.of("Apple", "Banana", "Cherry", "Date", "Elderberry");
// Convert the stream into an array of strings
String[] fruitArray = fruitStream.toArray(String[]::new);
// Print the array
for (String fruit : fruitArray) {
System.out.println(fruit);
}
}
}
Output:
Apple Banana Cherry Date Elderberry
Explanation:
In the provided example: We have a stream of fruit names. Using toArray(), we convert this stream into an array of strings. The String[]::new is a constructor reference used as the generator function to produce the output array of the desired type. We then iterate through the array and print each fruit.
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