The add(CharSequence newElement) of StringJoiner adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then “null” is added.
Syntax:
public StringJoiner add(CharSequence newElement)
Parameters: This method takes a mandatory parameter newElement which is the element to be added.
Returns: This method returns a reference to this StringJoiner
Below programs illustrate the add() method:
Example 1: To demonstrate add() with delimiter ” ”
Java
// Java program to demonstrate// add() method of StringJoinerimport java.util.StringJoiner;public class GFG1 { public static void main(String[] args) { // Creating StringJoiner with delimiter " " StringJoiner str = new StringJoiner(" "); // Adding elements in the StringJoiner str.add("Geeks"); str.add("for"); str.add("Geeks"); // Print the StringJoiner System.out.println(str.toString()); }} |
Geeks for Geeks
Example 2: To demonstrate add() with delimiter “,”
Java
// Java program to demonstrate// add() method of StringJoinerimport java.util.StringJoiner;public class GFG1 { public static void main(String[] args) { // Creating StringJoiner with delimiter "," StringJoiner str = new StringJoiner(","); // Adding elements in the StringJoiner str.add("Geeks"); str.add("for"); str.add("Geeks"); // Print the StringJoiner System.out.println(str.toString()); }} |
Geeks,for,Geeks

… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/stringjoiner-add-method-in-java/ […]