StringJoiner setEmptyValue() method in Java 8


The setEmptyValue() method of the StringJoiner class in Java 8 sets the sequence of characters. These characters are to be used when determining the string representation of this StringJoiner and when it is empty. That would be none of the elements have been added.

The syntax is as follows

public StringJoiner setEmptyValue(CharSequence emptyVal)

Here, emptyVal are the characters to return as the value of an empty StringJoiner

To work with the StringJoiner in Java 8, import the following package.

import java.util.StringJoiner;

The following is an example to implement StringJoiner setEmptyValue() method in Java:

Example

 Live Demo

import java.util.StringJoiner;
public class Demo {
   public static void main(String[] args) {
      StringJoiner strJoiner = new StringJoiner(",");
      System.out.println("StringJoiner is empty" + strJoiner);
      System.out.println(strJoiner.setEmptyValue("Empty!"));
      System.out.println(strJoiner);
      System.out.println("After Adding elements to the StringJoiner...");
      strJoiner.add("John");
      strJoiner.add("Tim");
      strJoiner.add("Jacob");
      strJoiner.add("Kevin");
      strJoiner.add("David");
      strJoiner.add("Tom");
      System.out.println(strJoiner);
   }
}

Output

StringJoiner is empty
Empty!
Empty!
After Adding elements to the StringJoiner...
John,Tim,Jacob,Kevin,David,Tom

Updated on: 30-Jul-2019

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements