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:
Java 8 Streams | Collectors.joining() method with Examples
Next article icon

Java 8 Streams | Collectors.joining() method with Examples

Last Updated : 17 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The joining() method of Collectors Class, in Java, is used to join various elements of a character or string array into a single string object. This method uses the stream to do so. There are various overloads of joining methods present in the Collector class. The class hierarchy is as follows: 

java.lang.Object   ↳ java.util.stream.Collectors

joining()

java.util.stream.Collectors.joining() is the most simple joining method which does not take any parameter. It returns a Collector that joins or concatenates the input streams into String in the order of their appearance.

Syntax:

public static Collector<CharSequence, ?, String> joining()

Illustration: Usage of joining() method

Program 1: Using joining() with an array of characters

In the below program, a character array is created in 'ch'. Then this array is fed to be converted into Stream using Stream.of(). Then the resulted stream is mapped for a sequential series using map(). At last, the sequential stream containing the character array is joined into a String using Collectors.joining() method. It is stored in the 'chString' variable.

Example 

Java
// Java Program to demonstrate the working // of the Collectors.joining() method  import java.util.stream.Collectors; import java.util.stream.Stream;  // Class public class GFG {      // Main driver method     public static void main(String[] args)     {          // Creating a custom character array         char[] ch = { 'G', 'e', 'e', 'k', 's', 'f', 'o',                       'r', 'G', 'e', 'e', 'k', 's' };          // Converting character array into string         // using joining() method of Collectors class         String chString             = Stream.of(ch)                   .map(arr -> new String(arr))                   .collect(Collectors.joining());          // Printing concatenated string         System.out.println(chString);     } } 

Output
GeeksforGeeks

Program 2: Using joining() with a list of characters

In the below program, a character list is created in 'ch'. Then this list is fed to be converted into Stream using ch.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last, the sequential stream containing the character list is joined into a String using Collectors.joining() method. It is stored in 'chString' variable. 

Example

Java
// Java Program to demonstrate Working of joining() Method // of Collectors Class  // Importing required classes import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;  // Main class public class GFG {      // Main driver method     public static void main(String[] args)     {         // Creating a character list         List<Character> ch = Arrays.asList(             'G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G',             'e', 'e', 'k', 's');          // Converting character list into string         // using joining() method of Collectors class         String chString             = ch.stream()                   .map(String::valueOf)                   .collect(Collectors.joining());          // Printing the concatenated string         System.out.println(chString);     } } 

Output
GeeksforGeeks

Program 3: Using joining() with n list of string

In the below program, a String list is created in 'str'. Then this list is fed to be converted into Stream using str.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last, the sequential stream containing the character list is joined into a String using Collectors.joining() method. It is stored in 'chString' variable. 

Example

Java
// Java Program to demonstrate the working // of the Collectors.joining() method  import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;  public class GFG {     public static void main(String[] args)     {         // Create a string list         List<String> str = Arrays.asList("Geeks", "for", "Geeks");          // Convert the string list into String         // using Collectors.joining() method         String chString             = str.stream().collect(Collectors.joining());          // Print the concatenated String         System.out.println(chString);     } } 

Output:

GeeksforGeeks

joining(delimiter)

java.util.stream.Collectors.joining(CharSequence delimiter) is an overload of joining() method which takes delimiter as a parameter, of the type CharSequence. A delimiter is a symbol or a CharSequence that is used to separate words from each other. For example, in every sentence, space ' ' is used as the default delimiter for the words in it. It returns a Collector that joins or concatenates the input elements into String in the order of their appearance, separated by the delimiter. 

Syntax:

public static Collector<CharSequence, ?, String> joining(CharSequence delimiter)

Below are the illustration for how to use joining(delimiter) method: 

Program 1: Using joining(delimiter) with a list of characters: In the below program, a character list is created in 'ch'. Then this list is fed to be converted into Stream using ch.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last, the sequential stream containing the character list is joined into a String using Collectors.joining() method with ", " passed as the delimiter. It is stored in 'chString' variable. 

Java
// Java Program to demonstrate the working // of the Collectors.joining() method  import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;  public class GFG {     public static void main(String[] args)     {         // Create a character list         List<Character> ch = Arrays.asList('G', 'e', 'e', 'k', 's', 'f',                            'o', 'r', 'G', 'e', 'e', 'k', 's');          // Convert the character list into String         // using Collectors.joining() method         // with, as the delimiter         String chString = ch.stream()                               .map(String::valueOf)                               .collect(Collectors.joining(", "));          // Print the concatenated String         System.out.println(chString);     } } 

Output:

G, e, e, k, s, f, o, r, G, e, e, k, s

Program 2: Using joining(delimiter) with a list of string: 

In the below program, a String list is created in 'str'. Then this list is fed to be converted into Stream using str.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last, the sequential stream containing the character list is joined into a String using Collectors.joining() method with ", " passed as the delimiter. It is stored in 'chString' variable. 

Java
// Java Program to demonstrate the working // of the Collectors.joining() method  import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;  public class GFG {     public static void main(String[] args)     {         // Create a string list         List<String> str = Arrays.asList("Geeks", "for", "Geeks");          // Convert the string list into String         // using Collectors.joining() method         String chString = str.stream().collect(             Collectors.joining(", "));          // Print the concatenated String         System.out.println(chString);     } } 

Output:

Geeks, for, Geeks

joining(delimiter, prefix, suffix)

java.util.stream.Collectors.joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) is an overload of joining() method which takes delimiter, prefix and suffix as parameter, of the type CharSequence. A delimiter is a symbol or a CharSequence that is used to separate words from each other. A prefix is a symbol or a CharSequence that is joined at the starting of the 1st element of the String. Then suffix is also a CharSequence parameter but this is joined after the last element of the string. i.e. at the end. For example, in every {Geeks, for, Geeks}, space ' ' is used as the by default delimiter for the words in it. The '{' is the prefix and '}' is the suffix. It returns a Collector that joins or concatenates the input elements into String in the order of their appearance, separated by the delimiter. 

Syntax:

public static Collector<CharSequence, ?, String> joining(CharSequence delimiter.                                                         CharSequence prefix,                                                        CharSequence suffix))

Below are the illustration for how to use joining(delimiter, prefix, suffix) method: 

Program 1: Using joining() with a list of characters: In the below program, a character list is created in 'ch'. Then this list is fed to be converted into Stream using ch.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last, the sequential stream containing the character list is joined into a String using Collectors.joining() method with ", " passed as the delimiter, "[" as the prefix and "]" as the suffix. It is stored in 'chString' variable. 

Java
// Java Program to demonstrate the working // of the Collectors.joining() method  import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;  public class GFG {     public static void main(String[] args)     {         // Create a character list         List<Character> ch = Arrays.asList('G', 'e', 'e', 'k', 's', 'f',                            'o', 'r', 'G', 'e', 'e', 'k', 's');          // Convert the character list into String         // using Collectors.joining() method         // with, as the delimiter         String chString             = ch.stream()                   .map(String::valueOf)                   .collect(Collectors.joining(", ", "[", "]"));          // Print the concatenated String         System.out.println(chString);     } } 

Output:

[G, e, e, k, s, f, o, r, G, e, e, k, s]

Program 2: Using joining() with a list of string: In the below program, a String list is created in 'str'. Then this list is fed to be converted into Stream using str.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last, the sequential stream containing the character list is joined into a String using Collectors.joining() method with ", " passed as the delimiter, "{" as the prefix and "}" as the suffix. It is stored in 'chString' variable. 

Java
// Java Program to demonstrate the working // of the Collectors.joining() method  import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;  public class GFG {     public static void main(String[] args)     {         // Create a string list         List<String> str = Arrays.asList("Geeks", "for", "Geeks");          // Convert the string list into String         // using Collectors.joining() method         String chString = str.stream().collect(Collectors.joining(", ", " {", "} "));          // Print the concatenated String         System.out.println(chString);     } } 

Output:

{Geeks, for, Geeks}

Next Article
Java 8 Streams | Collectors.joining() method with Examples

R

RishabhPrabhu
Improve
Article Tags :
  • Java
  • Java-Library
  • Java - util package
  • Java-Functions
  • java-stream
  • Java-Stream-Collectors
  • Java-Collectors
Practice Tags :
  • Java

Similar Reads

    Collectors toList() method in Java with Examples
    The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c
    2 min read
    Java streams counting() method with examples
    In Java 8, there is predefined counting() method returned by Collectors class counting() method to count the number of elements in a Stream. Syntax : public static Collector counting() Where, output is a Collector, acting on a Stream of elements of type T, with its finisher returning the ‘collected’
    2 min read
    Stream count() method in Java with examples
    long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers
    2 min read
    Collectors toSet() in Java with Examples
    Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter
    2 min read
    Stream dropWhile() method in Java with examples
    The dropWhile(java.util.function.Predicate) method returns two different types of stream depending upon whether the stream is ordered or not. If the stream is ordered then a stream of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate i
    3 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