Java StringJoiner class

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java StringJoiner

Java added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, you can create string by passing delimiters like comma(,), hyphen(-) etc. You can also pass prefix and suffix to the char sequence.

StringJoiner Constructors

Constructor

Description

Public StringJoiner(CharSequence delimiter)

It constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter. It throws NullPointerException if delimiter is null.

Public StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix)

It constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix. It throws NullPointerException if prefix, delimiter, or suffix is null.


StringJoiner Methods

Method

Description

Public StringJoiner add(CharSequence newElement)

It adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null,"null" is added.

Public StringJoiner merge(StringJoiner other)

It 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.

Public int length()

It returns the length of the String representation of this StringJoiner.

Public StringJoiner setEmptyValue(CharSequence emptyValue)

It sets the sequence of characters to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty.

Java StringJoiner Example

  1. // importing StringJoiner class  
  2. import java.util.StringJoiner;  
  3. public class StringJoinerExample {  
  4.     public static void main(String[] args) {  
  5.         StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter   
  6.           
  7.         // Adding values to StringJoiner  
  8.         joinNames.add("Rahul");  
  9.         joinNames.add("Raju");  
  10.         joinNames.add("Peter");  
  11.         joinNames.add("Raheem");  
  12.                   
  13.         System.out.println(joinNames);  
  14.     }  
  15. }  

Output:

Rahul,Raju,Peter,Raheem

Java StringJoiner Example: adding prefix and suffix

 
  1. // importing StringJoiner class  
  2. import java.util.StringJoiner;  
  3. public class StringJoinerExample {  
  4.     public static void main(String[] args) {  
  5.         StringJoiner joinNames = new StringJoiner(",""[""]");   // passing comma(,) and square-brackets as delimiter   
  6.           
  7.         // Adding values to StringJoiner  
  8.         joinNames.add("Rahul");  
  9.         joinNames.add("Raju");  
  10.         joinNames.add("Peter");  
  11.         joinNames.add("Raheem");  
  12.                   
  13.         System.out.println(joinNames);  
  14.     }  
  15. }  

Output:

[Rahul,Raju,Peter,Raheem]

StringJoiner Example: Merge Two StringJoiner

The merge() method merges two StringJoiner objects excluding of prefix and suffix of second StringJoiner object.

 
  1. // importing StringJoiner class  
  2. import java.util.StringJoiner;  
  3. public class StringJoinerExample {  
  4.     public static void main(String[] args) {  
  5.   
  6.         StringJoiner joinNames = new StringJoiner(",""[""]");   // passing comma(,) and square-brackets as delimiter   
  7.           
  8.         // Adding values to StringJoiner  
  9.         joinNames.add("Rahul");  
  10.         joinNames.add("Raju");  
  11.   
  12.         // Creating StringJoiner with :(colon) delimiter  
  13.         StringJoiner joinNames2 = new StringJoiner(":""[""]");  // passing colon(:) and square-brackets as delimiter   
  14.           
  15.         // Adding values to StringJoiner  
  16.         joinNames2.add("Peter");  
  17.         joinNames2.add("Raheem");  
  18.   
  19.         // Merging two StringJoiner  
  20.         StringJoiner merge = joinNames.merge(joinNames2);   
  21.         System.out.println(merge);  
  22.     }  
  23. }  

Output:

[Rahul,Raju,Peter:Raheem]

StringJoiner Example: StringJoiner Methods

 
  1. // importing StringJoiner class  
  2. import java.util.StringJoiner;  
  3. public class StringJoinerExample {  
  4.     public static void main(String[] args) {  
  5.         StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter   
  6.           
  7.         // Prints nothing because it is empty  
  8.         System.out.println(joinNames);  
  9.           
  10.         // We can set default empty value.  
  11.         joinNames.setEmptyValue("It is empty");  
  12.         System.out.println(joinNames);  
  13.           
  14.           
  15.         // Adding values to StringJoiner  
  16.         joinNames.add("Rahul");  
  17.         joinNames.add("Raju");  
  18.         System.out.println(joinNames);  
  19.           
  20.         // Returns length of StringJoiner  
  21.         int length = joinNames.length();  
  22.         System.out.println("Length: "+length);  
  23.           
  24.         // Returns StringJoiner as String type   
  25.         String str = joinNames.toString();  
  26.         System.out.println(str);  
  27.           
  28.         // Now, we can apply String methods on it  
  29.         char ch = str.charAt(3);  
  30.         System.out.println("Character at index 3: "+ch);  
  31.           
  32.         // Adding one more element   
  33.         joinNames.add("Sorabh");  
  34.         System.out.println(joinNames);  
  35.           
  36.         // Returns length  
  37.         int newLength = joinNames.length();  
  38.         System.out.println("New Length: "+newLength);  
  39.     }  
  40. }  

Output:

It is empty
Rahul,Raju
Length: 10
Rahul,Raju
Character at index 3: u
Rahul,Raju,Sorabh
New Length: 17