Thursday, September 18, 2025
HomeLanguagesJavaStringJoiner merge() method in Java

StringJoiner merge() method in Java

The merge(StringJoiner other) of StringJoiner adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.
Syntax: 
 

public StringJoiner merge(StringJoiner other)

Parameters: This method accepts a mandatory parameter other which is the StringJoiner whose contents should be merged into this one
Returns: This method returns this StringJoiner
Exception: This method throws NullPointerException if the other StringJoiner is null
Below programs illustrate the merge() method:
Example 1: To demonstrate merge() with delimiter ” “
 

Java




// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter " "
        StringJoiner str1 = new StringJoiner(" ");
 
        // Adding elements in the StringJoiner
        str1.add("Geeks");
        str1.add("for");
        str1.add("Geeks");
 
        // Print the StringJoiner
        System.out.println("StringJoiner 1: "
                           + str1.toString());
 
        // Creating the second StringJoiner
        StringJoiner str2 = new StringJoiner(" ");
 
        str2.add("A");
        str2.add("Computer");
        str2.add("Portal");
 
        // Print the second StringJoiner
        System.out.println("StringJoiner 2: "
                           + str2.toString());
 
        // Merging the StringJoiner using merge()
        StringJoiner str = str1.merge(str2);
 
        // Printing the merged StringJoiner
        System.out.println("Merged StringJoiner : "
                           + str);
    }
}


Output: 

StringJoiner 1: Geeks for Geeks
StringJoiner 2: A Computer Portal
Merged StringJoiner : Geeks for Geeks A Computer Portal

 

Example 2: To demonstrate merge() with delimiter “, “
 

Java




// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG1 {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter ", "
        StringJoiner str1 = new StringJoiner(", ");
 
        // Adding elements in the StringJoiner
        str1.add("Geeks");
        str1.add("for");
        str1.add("Geeks");
 
        // Print the StringJoiner
        System.out.println("StringJoiner 1: "
                           + str1.toString());
 
        // Creating the second StringJoiner
        StringJoiner str2 = new StringJoiner(", ");
 
        str2.add("A");
        str2.add("Computer");
        str2.add("Portal");
 
        // Print the StringJoiner
        System.out.println("StringJoiner 2: "
                           + str2.toString());
 
        // Merging the StringJoiner using merge()
        StringJoiner str = str1.merge(str2);
 
        // Printing the merged StringJoiner
        System.out.println("Merged StringJoiner : "
                           + str);
    }
}


Output: 

StringJoiner 1: Geeks, for, Geeks
StringJoiner 2: A, Computer, Portal
Merged StringJoiner : Geeks, for, Geeks, A, Computer, Portal

 

Example 3: To demonstrate NullPointerException
 

Java




// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG1 {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter ", "
        StringJoiner str1 = new StringJoiner(", ");
 
        // Adding elements in the StringJoiner
        str1.add("Geeks");
        str1.add("for");
        str1.add("Geeks");
 
        // Print the StringJoiner
        System.out.println("StringJoiner 1: "
                           + str1.toString());
 
        // Creating the second StringJoiner
        // to be merged as null
        StringJoiner str2 = null;
 
        // Print the StringJoiner
        System.out.println("StringJoiner 2: "
                           + str2);
 
        try {
 
            // Merging the StringJoiner using merge()
            StringJoiner str = str1.merge(str2);
        }
        catch (Exception e) {
            System.out.println("Exception during merge: "
                               + e);
        }
    }
}


Output: 

StringJoiner 1: Geeks, for, Geeks
StringJoiner 2: null
Exception during merge: java.lang.NullPointerException

 

RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6662 POSTS0 COMMENTS
Nicole Veronica
11835 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7053 POSTS0 COMMENTS
Thapelo Manthata
6736 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS