🎓 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 concat() method in Java programming and how to use it with an example.
1. Stream concat() Method Overview
Definition:
The Stream.concat() method is a static utility method that is used to concatenate two streams into a single stream. The resulting stream will contain elements from both input streams in the order they appear.
Syntax:
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
Parameters:
- a: the first stream.
- b: the second stream.
Key Points:
- It's a static method and does not operate on a stream instance.
- It is an intermediate operation, meaning the concatenation does not happen immediately. Instead, the operation is executed only when a terminal operation is invoked on the concatenated stream.
- If either stream is parallel, the resulting stream is also parallel.
- Closing the concatenated stream closes both input streams.
- Concatenation is lazy, meaning elements are consumed from the input streams as they are required.
- Care should be taken when using infinite streams with concat, as it can lead to unintended infinite sequences.
2. Stream concat() Method Example
import java.util.stream.Stream;
public class StreamConcatExample {
public static void main(String[] args) {
Stream<String> fruitsStream = Stream.of("Apple", "Banana", "Cherry");
Stream<String> veggiesStream = Stream.of("Artichoke", "Broccoli", "Carrot");
// Concatenate the two streams
Stream<String> concatenatedStream = Stream.concat(fruitsStream, veggiesStream);
// Print the concatenated stream
concatenatedStream.forEach(System.out::println);
}
}
Output:
Apple Banana Cherry Artichoke Broccoli Carrot
Explanation:
In the provided example:
We have two streams, one containing fruit names and another containing vegetable names.
Using Stream.concat(), we concatenate these two streams into a single stream.
We then print the concatenated stream, which first displays fruits followed by vegetables.
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