Java.util.StringJoiner in Java8

StringJoiner is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Though this can also be with the help of StringBuilder class to append delimiter after each string, but StringJoiner provides easy way to do that without much code to write.
Constructors :

    1. StringJoiner(CharSequence delimiter) : Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.
      Syntax : 
      public StringJoiner(CharSequence delimiter)
      Parameters : 
      delimiter - the sequence of characters to be used between 
      each element added to the StringJoiner value
      Throws:
      NullPointerException - if delimiter is null
      
    2. StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix) : Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.
      Syntax : 
      public StringJoiner(CharSequence delimiter, 
             CharSequence prefix, CharSequence suffix)
      Parameters : 
      delimiter - the sequence of characters to be used between
      each element added to the StringJoiner value
      prefix - the sequence of characters to be used at the beginning
      suffix - the sequence of characters to be used at the end
      Throws:
      NullPointerException - if prefix, delimiter, or suffix is null
      

Methods : There are 5 methods in StringJoiner class.

    1. String toString() : This method returns String object of this StringJoiner.
      Syntax : 
      public String toString()
      Parameters : 
      NA
      Returns :
      the string representation of this StringJoiner
      Overrides :
      toString in class Object
      
    2. StringJoiner add(CharSequence newElement) : This method adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then “null” is added.
      Syntax : 
      public StringJoiner add(CharSequence newElement)
      Parameters : 
      newElement - The element to add
      Returns :
      a reference to this StringJoiner
      
    3. StringJoiner merge(StringJoiner other) : This method adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect. If the other StringJoiner is using a different delimiter, then elements from the other StringJoiner are concatenated with that delimiter and the result is appended to this StringJoiner as a single element.
      Syntax : 
      public StringJoiner merge(StringJoiner other)
      Parameters : 
      other - The StringJoiner whose contents should be merged
      into this one
      Returns :
      This StringJoiner
      Throws :
      NullPointerException - if the other StringJoiner is null
      
    4. int length() : This method returns the length of the String representation of this StringJoiner.
      Syntax : 
      public int length()
      Parameters : 
      NA
      Returns :
      This StringJoiner
      
    5. StringJoiner setEmptyValue(CharSequence emptyValue) : This method sets string to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty.
      Syntax : 
      public StringJoiner setEmptyValue(CharSequence emptyValue)
      Parameters : 
      emptyValue - the characters to return as the value 
      of an empty StringJoiner
      Returns :
      this StringJoiner itself so the calls may be chained
      Throws:
      NullPointerException - when the emptyValue parameter is null
      

Below is the java program to demonstrate all methods.

// Java program to demonstrate methods
// of StringJoiner class

import java.util.ArrayList;
import java.util.StringJoiner;
	
public class Test2
{
	public static void main(String[] args)
	{
	   ArrayList<String> al = new ArrayList<>();
	   
	   al.add("Ram");
	   al.add("Shyam");
	   al.add("Alice");
	   al.add("Bob");
	   
	   StringJoiner sj1 = new StringJoiner(",");
	   
	   // setEmptyValue() method
       sj1.setEmptyValue("sj1 is empty");
	   System.out.println(sj1);
	   
	   // add() method
	   sj1.add(al.get(0)).add(al.get(1));
	   System.out.println(sj1);
	   
	   // length() method
	   System.out.println("Length of sj1 : " + sj1.length());
	   
	   StringJoiner sj2 = new StringJoiner(":");
	   sj2.add(al.get(2)).add(al.get(3));
	   
	   //merge() method
	   sj1.merge(sj2);
	   
	   // toString() method
	   System.out.println(sj1.toString());
	   
	   System.out.println("Length of new sj1 : " + sj1.length());
	
	}
}

Output:

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 contribute.geeksforgeeks.org or mail your article to contribute@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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.