La combinación (StringJoiner other) de StringJoiner agrega el contenido del StringJoiner dado sin prefijo y sufijo como el siguiente elemento si no está vacío. Si el StringJoiner dado está vacío, la llamada no tiene efecto.
Sintaxis:
public StringJoiner merge(StringJoiner other)
Parámetros: este método acepta otro parámetro obligatorio, que es StringJoiner , cuyo contenido debe fusionarse con este
. Devuelve: este método devuelve este StringJoiner
. Excepción: este método genera una excepción NullPointerException si el otro StringJoiner es nulo
.
1: Para demostrar merge() con delimitador ” “
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); } }
Producción:
StringJoiner 1: Geeks for Geeks StringJoiner 2: A Computer Portal Merged StringJoiner : Geeks for Geeks A Computer Portal
Ejemplo 2: Para demostrar merge() con delimitador “, “
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); } }
Producción:
StringJoiner 1: Geeks, for, Geeks StringJoiner 2: A, Computer, Portal Merged StringJoiner : Geeks, for, Geeks, A, Computer, Portal
Ejemplo 3: Para demostrar 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); } } }
Producción:
StringJoiner 1: Geeks, for, Geeks StringJoiner 2: null Exception during merge: java.lang.NullPointerException