StringJoiner Class in Java

24 Jan, 2022
StringJoiner is a class in java.util package is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a given suffix. Though this can also be done with the help of the StringBuilder class to append delimiter after each string, StringJoiner provides an easy way to do that without much code to write. 
1. StringJoiner(CharSequence delimiter): It constructs a StringJoiner with no characters, no prefix or suffix, and a copy of the supplied delimiter. 
Syntax: 
public StringJoiner(CharSequence delimiter)
Parameters: The sequence of characters to be used between each element added to the StringJoiner value
Exception Thrown: NullPointerException if the delimiter is null
2. StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix): It constructs a StringJoiner with no characters using copies of the supplied prefix, delimiter, and suffix. If no characters are added to the StringJoiner and methods accessing the string value are invoked, it will return the prefix + suffix (or properties thereof) in the result unless setEmptyValue has first been called. 
Syntax: 
public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Parameters: 
The sequence of characters to be used between each element added to the StringJoiner value
The sequence of characters to be used at the beginning
The sequence of characters to be used at the end
Exception Thrown: NullPointerException if prefix, delimiter, or suffix is null.
 Example:
Java
// Java program to Demonstrate Methods// of StringJoiner class // Importing required classesimport java.util.ArrayList;import java.util.StringJoiner; // Main classpublic class GFG {     // Main driver method    public static void main(String[] args)    {        // Creating an empty ArrayList of string type        ArrayList<String> al = new ArrayList<>();         // Adding elements to above List        al.add("Ram");        al.add("Shyam");        al.add("Alice");        al.add("Bob");         // Creating object of class inside main()        StringJoiner sj1 = new StringJoiner(",");         // Using setEmptyValue() method        sj1.setEmptyValue("sj1 is empty");        System.out.println(sj1);         // Using add() method        sj1.add(al.get(0)).add(al.get(1));        System.out.println(sj1);         // Using length() method        System.out.println("Length of sj1 : "                           + sj1.length());         StringJoiner sj2 = new StringJoiner(":");        sj2.add(al.get(2)).add(al.get(3));         // Using merge() method        sj1.merge(sj2);         // Using toString() method        System.out.println(sj1.toString());         System.out.println("Length of new sj1 : "                           + sj1.length());    }}
sj1 is empty
Ram,Shyam
Length of sj1 : 9
Ram,Shyam,Alice:Bob
Length of new sj1 : 19
 This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
payal soni
romeosaurabh
nishkarshgandhi
solankimayank
Java - util package
Java-StringJoiner
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java