Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Stream.concat() in Java
Next article icon

Stream.concat() in Java

Last Updated : 06 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
Stream.concat() method creates a concatenated stream in which the elements are all the elements of the first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel. Syntax :
  static <T> Stream<T> concat(Stream<? extends T> stream1,                               Stream<? extends T> stream2)    Where, T is the type of stream elements,  stream1 represents the first stream,  stream2 represents the second stream and  the function returns the concatenation of  the two input streams  
The calls to Stream.concat(stream1, stream2) can be think of as forming a binary tree. The concatenation of all the input streams is at the root. The individual input streams are at the leaves. Below given are some examples of trees upto four input streams a, b, c and d. For two streams a and b, the tree looks like : For three streams a, b and c, the tree looks like : For four streams a, b, c and d, the tree looks like : Each additional input stream adds one layer of depth to the tree and one layer of indirection to reach all the other streams. Note : The elements returned by Stream.concat() method is ordered. For example, the following two lines returns the same result:
  Stream.concat(Stream.concat(stream1, stream2), stream3);  Stream.concat(stream1, Stream.concat(stream2, stream3));  
But the result for the following two are different.
  Stream.concat(Stream.concat(stream1, stream2), stream3);   Stream.concat(Stream.concat(stream2, stream1), stream3);  
Below are some examples to understand the implementation of the function in a better way. Example 1 : Java
// Implementation of Stream.concat() // method in Java 8 with 2 Streams import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream;  class GFG {      // Driver code     public static void main(String[] args)     {         // Creating two Streams         Stream<String> stream1 = Stream.of("Geeks", "for");         Stream<String> stream2 = Stream.of("GeeksQuiz", "GeeksforGeeks");          // concatenating both the Streams         // with Stream.concat() function         // and displaying the result         Stream.concat(stream1, stream2)             .forEach(element -> System.out.println(element));     } } 
Output:
  Geeks  for  GeeksQuiz  GeeksforGeeks  
Example 2 : Java
// Implementation of Stream.concat() // method in Java 8 with more than // two Streams import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream;  class GFG {      // Driver code     public static void main(String[] args)     {          // Creating more than two Streams         Stream<String> stream1 = Stream.of("Geeks");         Stream<String> stream2 = Stream.of("GeeksQuiz");         Stream<String> stream3 = Stream.of("GeeksforGeeks");         Stream<String> stream4 = Stream.of("GFG");          // concatenating all the Streams         // with Stream.concat() function         // and displaying the result         Stream.concat(Stream.concat(Stream.concat(stream1,                              stream2), stream3), stream4)             .forEach(element -> System.out.println(element));     } } 
Output:
  Geeks  GeeksQuiz  GeeksforGeeks  GFG  
Example 3 : Java
// Implementation of Stream.concat() // method in Java 8 with DoubleStream import java.util.*; import java.util.stream.Stream; import java.util.stream.DoubleStream;  class GFG {      // Driver code     public static void main(String[] args)     {          // Creating two Streams         DoubleStream Stream1 = DoubleStream.of(1520, 1620);         DoubleStream Stream2 = DoubleStream.of(1720, 1820);          // concatenating both the Streams and         // displaying the result         DoubleStream.concat(Stream1, Stream2)             .forEach(element -> System.out.println(element));     } } 
Output:
  1520.0  1620.0  1720.0  1820.0  
Example 4 : Java
// Implementation of Stream.concat() // method in Java 8 and removing // the duplicates import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream;  class GFG {      // Driver code     public static void main(String[] args)     {          // Creating two Streams         Stream<String> stream1 = Stream.of("Geeks", "for", "GeeksforGeeks");         Stream<String> stream2 = Stream.of("GeeksQuiz", "GeeksforGeeks", "for");          // concatenating both the Streams         // with Stream.concat() function         // and displaying the result after         // removing the duplicates         Stream.concat(stream1, stream2).distinct().forEach(element -> System.out.println(element));     } } 
Output:
  Geeks  for  GeeksforGeeks  GeeksQuiz  
Example 5 : Java
// Implementation of Stream.concat() // method in Java 8 with LongStream import java.util.*; import java.util.stream.Stream; import java.util.stream.LongStream;  class GFG {      // Driver code     public static void main(String[] args)     {          // Creating two Streams         LongStream Stream1 = LongStream.of(1520, 1620);         LongStream Stream2 = LongStream.of(1720, 1820);          // concatenating both the Streams and         // displaying the result         LongStream.concat(Stream1, Stream2)             .forEach(element -> System.out.println(element));     } } 
Output:
  1520  1620  1720  1820  

Next Article
Stream.concat() in Java

S

Sahil_Bansall
Improve
Article Tags :
  • Misc
  • Java
  • Java - util package
  • Java-Functions
  • java-stream
  • Java-Stream interface
Practice Tags :
  • Java
  • Misc

Similar Reads

    Stream In Java
    Stream was introduced in Java 8, the Stream API is used to process collections of objects. A stream in Java is a sequence of objects that supports various methods that can be pipelined to produce the desired result. Use of Stream in JavaThe uses of Stream in Java are mentioned below:Stream API is a
    7 min read
    Convert Stream to Set in Java
    Below given are some methods which can be used to convert Stream to Set in Java. Method 1 : Using Collectors Stream collect() method takes elements from a stream and stores them in a collection.collect(Collector.toSet()) collects elements from a stream to a Set. Stream.collect() method can be used t
    3 min read
    Convert a Set to Stream in Java
    Set interface extends Collection interface and Collection has stream() method that returns a sequential stream of the collection. Below given are some examples to understand the implementation in a better way. Example 1 : Converting Integer HashSet to Stream of Integers. Java // Java code for conver
    2 min read
    Array to Stream in Java
    Prerequisite : Stream In Java Using Arrays.stream() : Syntax : public static <T> Stream<T> getStream(T[] arr) { return Arrays.stream(arr); } where, T represents generic type. Example 1 : Arrays.stream() to convert string array to stream. Java // Java code for converting string array // t
    3 min read
    IntStream concat() in Java
    IntStream concat() method creates a concatenated stream in which the elements are all the elements of the first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences