StringJoiner setEmptyValue() method in Java

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.

Syntax:

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:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// setEmptyValue() method of StringJoiner
  
import java.util.StringJoiner;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Create a StringJoiner
        StringJoiner str = new StringJoiner(" ");
  
        // Print the empty StringJoiner
        System.out.println("Initial StringJoiner: "
                           + str);
  
        // Add an emptyValue
        // using setEmptyValue() method
        str.setEmptyValue("StrigJoiner is empty");
  
        // Print the StringJoiner
        System.out.println("After setEmptyValue(): "
                           + str);
  
        // Add elements to StringJoiner
        str.add("Geeks");
        str.add("forGeeks");
  
        // Print the StringJoiner
        System.out.println("Final StringJoiner: "
                           + str);
    }
}

chevron_right


Output:

Initial StringJoiner: 
After setEmptyValue(): StrigJoiner is empty
Final StringJoiner: Geeks forGeeks

Example 2: To demonstrate NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// setEmptyValue() method of StringJoiner
  
import java.util.StringJoiner;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Create a StringJoiner
        StringJoiner str = new StringJoiner(" ");
  
        // Print the empty StringJoiner
        System.out.println("Initial StringJoiner: "
                           + str);
  
        try {
            // Add a null emptyValue
            // using setEmptyValue() method
            str.setEmptyValue(null);
        }
        catch (Exception e) {
            System.out.println("Exception when adding null"
                               + " in setEmptyValue(): " + e);
        }
    }
}

chevron_right


Output:

Initial StringJoiner: 
Exception when adding null in setEmptyValue(): 
    java.lang.NullPointerException: 
    The empty value must not be null


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.