Skip to content
Related Articles

Related Articles

When to use StringJoiner over StringBuilder?
  • Difficulty Level : Easy
  • Last Updated : 11 Dec, 2018

Prerequisite: StringJoiner

StringJoiner is very useful, when you need to join Strings in a Stream.
Task : Suppose we want the string “[George:Sally:Fred]”, where we have given a string array that contains “George”, “Sally” and “Fred”.

StringJoiner provide add(String str) method to concatenate the strings based on supplied delimiter,prefix and suffix in the constructor, but if we use StringBuilder to perform our task then first we have to append prefix and then iterate through string array and append the required delimiter after each element and finally append the prefix. Below is the java program to demonstrate both ways.




// Java program to demonstrate use of 
// StringJoiner class over StringBuilder class
  
import java.util.StringJoiner;
  
public class Test 
{
    public static void main(String[] args)
    {
        // given string array
        String str[] = {"George","Sally","Fred"};
              
        // By using StringJoiner class
              
        // initializing StringJoiner instance with
        // required delimiter, prefix and suffix
        StringJoiner sj = new StringJoiner(":", "[", "]");
              
        // concatenating strings
        sj.add("George").add("Sally").add("Fred");
              
        // converting StringJoiner to String
        String desiredString = sj.toString();
              
        System.out.println(desiredString);
              
        // By using StringBuilder class
              
        // declaring empty stringbuilder
        StringBuilder sb = new StringBuilder();
              
        // appending prefix
        sb.append("[");
              
        // cheking for empty string array
        if(str.length>0)
        {
            // appending first element
            sb.append(str[0]);
                  
            // iterating through string array 
            // and appending required delimiter
            for (int i = 1; i < str.length; i++) 
            {
                sb.append(":").append(str[i]);
            }
        }
              
        // finally appending sufix
        sb.append("]");
              
        // converting StringBuilder to String
        String desiredString1 = sb.toString();
              
        System.out.println(desiredString1);
    }
}

Output:

[George:Sally:Fred]
[George:Sally:Fred]

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :