Java 8 StringJoiner Class

1. Overview

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 a string by passing delimiters like a comma(,), hyphen(-) etc. 
You can also pass prefix and suffix to the char sequence. In this post, we will learn how to use StringJoiner with examples.
Java 8 StringJoiner Class

2. Java 8 StringJoiner Class Example

2.1 StringJoiner Example: Simple Delimiters Example

 private static void delimiterDemonstration() {
  StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter
  // Adding values to StringJoiner
  joinNames.add("Rahul");
  joinNames.add("Raju");
  joinNames.add("Peter");
  joinNames.add("Raheem");
  System.out.println(joinNames);

  joinNames = new StringJoiner("|"); // passing comma(,) as delimiter

  // Adding values to StringJoiner
  joinNames.add("Rahul");
  joinNames.add("Raju");
  joinNames.add("Peter");
  joinNames.add("Raheem");
  System.out.println(joinNames);
}

2.2 StringJoiner Example: adding prefix and suffix

private static void addingPrefixAndSuffix() {
  // passing comma(,) and
  // square-brackets as
  // delimiter
  StringJoiner joinNames = new StringJoiner(",", "[", "]"); 
  // Adding values to StringJoiner
  joinNames.add("Rahul");
  joinNames.add("Raju");
  joinNames.add("Peter");
  joinNames.add("Raheem");

  System.out.println(joinNames);
}

2.3 StringJoiner Example: Merge Two StringJoiner

The merge() method merges two StringJoiner objects excluding of prefix and suffix of the second StringJoiner object.
private static void mergeTwoStringJoiner(){
  // passing comma(,) and square-brackets as delimiter
  StringJoiner joinNames = new StringJoiner(",", "[", "]");  
        
        // Adding values to StringJoiner  
        joinNames.add("Rahul");  
        joinNames.add("Raju");  
  
        // Creating StringJoiner with :(colon) delimiter  
        StringJoiner joinNames2 = new StringJoiner(":", "[", "]");  // passing colon(:) and square-brackets as delimiter   
          
        // Adding values to StringJoiner  
        joinNames2.add("Peter");  
        joinNames2.add("Raheem");  
  
        // Merging two StringJoiner  
        StringJoiner merge = joinNames.merge(joinNames2);   
        System.out.println(merge);  
 }

2.4 StringJoiner Example: StringJoiner Methods

private static void stringJoinerMethods(){
   StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter   
         
         // Prints nothing because it is empty  
         System.out.println(joinNames);  
           
         // We can set default empty value.  
         joinNames.setEmptyValue("It is empty");  
         System.out.println(joinNames);  
           
           
         // Adding values to StringJoiner  
         joinNames.add("Rahul");  
         joinNames.add("Raju");  
         System.out.println(joinNames);  
           
         // Returns length of StringJoiner  
         int length = joinNames.length();  
         System.out.println("Length: "+length);  
           
         // Returns StringJoiner as String type   
         String str = joinNames.toString();  
         System.out.println(str);  
           
         // Now, we can apply String methods on it  
         char ch = str.charAt(3);  
         System.out.println("Character at index 3: "+ch);  
           
         // Adding one more element   
         joinNames.add("Sorabh");  
         System.out.println(joinNames);  
           
         // Returns length  
         int newLength = joinNames.length();  
         System.out.println("New Length: "+newLength);  
 }

3. Complete Example for Reference


import java.util.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
 * @author RAMESH
 *
 */
public class StringJoinerClassExample {

 public static void main(String[] args) {
  delimiterDemonstration();
  addingPrefixAndSuffix();
  mergeTwoStringJoiner();
  stringJoinerMethods();
 }

 private static void delimiterDemonstration() {
  StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter
  // Adding values to StringJoiner
  joinNames.add("Rahul");
  joinNames.add("Raju");
  joinNames.add("Peter");
  joinNames.add("Raheem");
  System.out.println(joinNames);

  joinNames = new StringJoiner("|"); // passing comma(,) as delimiter

  // Adding values to StringJoiner
  joinNames.add("Rahul");
  joinNames.add("Raju");
  joinNames.add("Peter");
  joinNames.add("Raheem");
  System.out.println(joinNames);
 }

 private static void addingPrefixAndSuffix() {
  // passing comma(,) and
  // square-brackets as
  // delimiter
  StringJoiner joinNames = new StringJoiner(",", "[", "]"); 
  // Adding values to StringJoiner
  joinNames.add("Rahul");
  joinNames.add("Raju");
  joinNames.add("Peter");
  joinNames.add("Raheem");

  System.out.println(joinNames);
 }
 
 private static void mergeTwoStringJoiner(){
  // passing comma(,) and square-brackets as delimiter
  StringJoiner joinNames = new StringJoiner(",", "[", "]");  
        
        // Adding values to StringJoiner  
        joinNames.add("Rahul");  
        joinNames.add("Raju");  
  
        // Creating StringJoiner with :(colon) delimiter  
        StringJoiner joinNames2 = new StringJoiner(":", "[", "]");  // passing colon(:) and square-brackets as delimiter   
          
        // Adding values to StringJoiner  
        joinNames2.add("Peter");  
        joinNames2.add("Raheem");  
  
        // Merging two StringJoiner  
        StringJoiner merge = joinNames.merge(joinNames2);   
        System.out.println(merge);  
 }
 
 private static void stringJoinerMethods(){
   StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter   
         
         // Prints nothing because it is empty  
         System.out.println(joinNames);  
           
         // We can set default empty value.  
         joinNames.setEmptyValue("It is empty");  
         System.out.println(joinNames);  
           
           
         // Adding values to StringJoiner  
         joinNames.add("Rahul");  
         joinNames.add("Raju");  
         System.out.println(joinNames);  
           
         // Returns length of StringJoiner  
         int length = joinNames.length();  
         System.out.println("Length: "+length);  
           
         // Returns StringJoiner as String type   
         String str = joinNames.toString();  
         System.out.println(str);  
           
         // Now, we can apply String methods on it  
         char ch = str.charAt(3);  
         System.out.println("Character at index 3: "+ch);  
           
         // Adding one more element   
         joinNames.add("Sorabh");  
         System.out.println(joinNames);  
           
         // Returns length  
         int newLength = joinNames.length();  
         System.out.println("New Length: "+newLength);  
 }
}
Examples from java doc: The String "[George:Sally:Fred]" may be constructed as follows:
 StringJoiner sj = new StringJoiner(":", "[", "]");
 sj.add("George").add("Sally").add("Fred");
 String desiredString = sj.toString();
StringJoiner may be employed to create formatted output from a Stream using Collectors.joining(CharSequence).
For example:
 List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
 String commaSeparatedNumbers = numbers.stream()
     .map(i -> i.toString())
     .collect(Collectors.joining(", "));
The source code of this post is available on GitHub Repository.

4. Related Java 8 Top Posts

Comments