The setEmptyValue(CharSequence emptyValue) of StringJoiner sets the sequence of characters to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty. A copy of the emptyValue parameter is made for this purpose. Note that once an add method has been called, the StringJoiner is no longer considered empty, even if the element(s) added correspond to the empty String.
public StringJoiner setEmptyValue(CharSequence emptyValue)
Parameters: This method accepts a mandatory parameter emptyValue which is the characters to return as the value of an empty StringJoiner
Returns: This method returns this StringJoiner itself, so the calls may be chained
Exception: This method throws NullPointerException when the emptyValue parameter is null
Below examples illustrates the setEmptyValue() method:
Initial StringJoiner: After setEmptyValue(): StrigJoiner is empty Final StringJoiner: Geeks forGeeks
Example 2: To demonstrate NullPointerException
Initial StringJoiner: Exception when adding null in setEmptyValue(): java.lang.NullPointerException: The empty value must not be null
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.