When to use StringJoiner over StringBuilder? Last Updated : 11 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Prerequisite: StringJoinerStringJoiner 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 // 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("["); // checking 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 suffix sb.append("]"); // converting StringBuilder to String String desiredString1 = sb.toString(); System.out.println(desiredString1); } } Output: [George:Sally:Fred] [George:Sally:Fred] Comment More infoAdvertise with us Next Article When to use StringJoiner over StringBuilder? K kartik Improve Article Tags : Java Java-Library Java-Strings Java-String-Programs Practice Tags : JavaJava-Strings Similar Reads When to use Rope over StringBuilder in Java? What are Ropes? Rope is a binary tree data structure where each node except the leaf contains the number of characters present to the left of the node. They are mainly used by text editors to store and manipulate large strings. It provides different string operations such as append, insert and delet 3 min read StringBuffer vs StringBuilder in Java Java provides three classes to represent the sequence of characters i.e. String, StringBuffer, and StringBuilder. A string is immutable in Java whereas, both StringBuffer and StringBuilder are mutable. This means they allow modifications to the character sequence without creating new objects.The mai 4 min read String vs StringBuilder vs StringBuffer in Java A string is a sequence of characters. In Java, String objects are immutable, which simply means once created, their values can not be changed. In Java, String, StringBuilder, and StringBuffer are used for handling strings. The main difference is:String: Immutable, meaning its value cannot be changed 6 min read StringJoiner toString() method in Java The toString() of StringJoiner is used to convert StringJoiner to String. It returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are 2 min read StringBuilder trimToSize() method in Java with Examples The trimToSize() method of StringBuilder class is the inbuilt method used to trims the capacity used for the character sequence of StringBuilder object. If the buffer used by StringBuilder object is larger than necessary to hold its current sequence of characters, then this method is called to resiz 2 min read Like