🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 | 
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|PrabhasStringJoiner 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: 17Complete 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: 17Source code on GitHub
The source code of this post is available on GitHub Repository.
4. Related Java 8 Top Posts
- Java 8 Lambda Expressions
- Java 8 Functional Interfaces
- Java 8 Method References
- Java 8 Stream API
- Java 8 Optional Class
- Java 8 Collectors Class
- Java 8 StringJoiner Class
- Java 8 Static and Default Methods in Interface
- Guide to Java 8 forEach Method
- Handle NullPointerException in Controller, Service and DAO Layer using Java 8 Optional Class
- How to Use Java 8 Stream API in Java Projects
- Migrating Source Code to Java 8
 
 
 
![[NEW] Full-Stack Java Development with Spring Boot 3 & React Build 5 Spring Boot Projects with Java: Line-by-Line Coding](https://img-c.udemycdn.com/course/750x422/5338984_4d3a_5.jpg) 
 
 
 
 
 
 
 
 
 
 
Comments
Post a Comment
Leave Comment