Java 8 Streams | Collectors.joining() method with Examples
Last Updated :
17 Jan, 2023
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);
}
}
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);
}
}
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}
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read