Java.util.StringJoiner in Java 8 Example Ans:I hope everybody knows about it.i am just giving my inputs on it.So StringJoiner is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Though, this can also be with the help of StringBuilder class to append delimiter after each string, but StringJoiner provides easy way to do that without much code to write. Constructors of StringJoiner : 1)StringJoiner(CharSequence delimiter) : Small description of it. Syntax : public StringJoiner(CharSequence delimiter) Parameters : delimiter - the sequence of characters to be used between each element added to the StringJoiner value Throws: NullPointerException - if delimiter is null 2)StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix) : Syntax : public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) Parameters : delimiter - the sequence of characters to be used between each element added to the StringJoiner value prefix - the sequence of characters to be used at the beginning suffix - the sequence of characters to be used at the end Throws: NullPointerException - if prefix, delimiter, or suffix is null Methods : There are 5 methods in StringJoiner class. 1)String toString() : Syntax : public String toString() Parameters : NA Returns : the string representation of this StringJoiner Overrides : toString in class Object 2)StringJoiner add(CharSequence newElement) : Syntax : public StringJoiner add(CharSequence newElement) Parameters : newElement - The element to add Returns : a reference to this StringJoiner 3)StringJoiner merge(StringJoiner other) : Syntax : public StringJoiner merge(StringJoiner other) Parameters : other - The StringJoiner whose contents should be merged into this one Returns : This StringJoiner Throws : NullPointerException - if the other StringJoiner is null 4)int length() : Syntax : public int length() Parameters : NA Returns : This StringJoiner 5)StringJoiner setEmptyValue(CharSequence emptyValue) : Syntax : public StringJoiner setEmptyValue(CharSequence emptyValue) Parameters : emptyValue - the characters to return as the value of an empty StringJoiner Returns : this StringJoiner itself so the calls may be chained Throws: NullPointerException - when the emptyValue parameter is null Sample Program for StringJoiner: import java.util.ArrayList; import java.util.StringJoiner; public class SampleProgram { public static void main(String[] args) { ArrayList<String> al = new ArrayList<>(); al.add("Abhinaw"); al.add("Abhishek"); al.add("Ram"); al.add("VirtualRelaity"); StringJoiner sj1 = new StringJoiner(","); // setEmptyValue() method sj1.setEmptyValue("sj1 is empty"); System.out.println(sj1); // add() method sj1.add(al.get(0)).add(al.get(1)); System.out.println(sj1); // length() method System.out.println("Length of sj1 : " + sj1.length()); StringJoiner sj2 = new StringJoiner(":"); sj2.add(al.get(2)).add(al.get(3)); //merge() method sj1.merge(sj2); // toString() method System.out.println(sj1.toString()); System.out.println("Length of new sj1 : " + sj1.length()); } } Output: sj1 is empty Abhinaw,Abhishek Length of sj1 : 16 Abhinaw,Abhishek,Ram:VirtualRelaity Length of new sj1 : 35