Java 8 StringJoiner Class with Examples

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. 

Java 8 StringJoiner Class
Java 8 StringJoiner Class Examples

You can also pass prefix and suffix to the char sequence. In this post, we will learn how to use StringJoiner with examples.

StringJoiner Example 1: Simple Delimiters Example

import java.util.StringJoiner;

public class StringJoinerExample{

     public static void main(String []args){
        delimiterDemonstration();
     }
     
     private static void delimiterDemonstration() {
        StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter
     // Adding values to StringJoiner
        joinNames.add("John");
        joinNames.add("Tony");
        joinNames.add("Amir");
        joinNames.add("Prabhas");
        System.out.println(joinNames);

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

        // Adding values to StringJoiner
        joinNames.add("John");
        joinNames.add("Tony");
        joinNames.add("Amir");
        joinNames.add("Prabhas");
        System.out.println(joinNames);
    }
}

Output:

John,Tony,Amir,Prabhas
John|Tony|Amir|Prabhas

StringJoiner Example 2: adding prefix and suffix

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) {
     addingPrefixAndSuffix();
 }

 private static void addingPrefixAndSuffix() {
         // passing comma(,) and
        // square-brackets as
        // delimiter
         StringJoiner joinNames = new StringJoiner(",", "[", "]"); 
         // Adding values to StringJoiner
        joinNames.add("Ramesh");
         joinNames.add("Tony");
        joinNames.add("Stark");
         joinNames.add("John");

         System.out.println(joinNames);
     }
}

Output:

[Ramesh,Tony,Stark,John]

StringJoiner Example 3: Merge Two StringJoiner

The merge() method merges two StringJoiner objects excluding of prefix and suffix of the second StringJoiner object.
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) {
     mergeTwoStringJoiner();
 }
 
 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  
        joinNames.add("Stark");
        joinNames.add("John");
  
        // Merging two StringJoiner  
        StringJoiner merge = joinNames.merge(joinNames2);   
        System.out.println(merge);  
 }
}

Output:

[Rahul,Raju,Stark,John]

StringJoiner Example 4: StringJoiner Methods

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) {
     stringJoinerMethods();
 }
 
 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);  
     }
}

Output:

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

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("Ramesh");
    joinNames.add("Tony");
    joinNames.add("Stark");
    joinNames.add("John");
    System.out.println(joinNames);

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

  // Adding values to StringJoiner
    joinNames.add("Ramesh");
    joinNames.add("Tony");
    joinNames.add("Stark");
    joinNames.add("John");
    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("Ramesh");
  joinNames.add("Tony");
  joinNames.add("Stark");
  joinNames.add("John");

  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  
        joinNames.add("Stark");
        joinNames.add("John");
  
        // 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);  
     }
}

Output:

Ramesh,Tony,Stark,John
Ramesh|Tony|Stark|John
[Ramesh,Tony,Stark,John]
[Rahul,Raju,Stark,John]

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

Source code on GitHub

The source code of this post is available on GitHub Repository.

4. Related Java 8 Top Posts

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments