Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
StringJoiner merge() method in Java 8
The merge() method of the StringJoiner class in Java 8 is used to merge the contents of the StringJoiner str, which is passed as a parameter. The content gets added as the next element.
The syntax is as follows:
public StringJoiner merge(StringJoiner str)
Here, str is the StringJoiner content to be merged.
To work with the StringJoiner in Java 8, import the following package:
import java.util.StringJoiner;
The following is an example to implement StringJoiner merge() method in Java:
Example
import java.util.StringJoiner;
public class Demo {
public static void main(String[] args) {
// StringJoiner 1
StringJoiner strJoin1 = new StringJoiner(" ");
strJoin1.add("Asia");
strJoin1.add("is");
strJoin1.add("a");
strJoin1.add("continent");
strJoin1.add("and");
System.out.println("StringJoiner 1 = "+strJoin1.toString());
// StringJoiner 2
StringJoiner strJoin2 = new StringJoiner(" ");
strJoin2.add("Tokyo");
strJoin2.add("is");
strJoin2.add("a");
strJoin2.add("city");
System.out.println("StringJoiner 2 = "+strJoin2.toString());
StringJoiner mergedStr = strJoin1.merge(strJoin2);
System.out.println("New StringJoiner (strJoin1 + strJoin2) = " + mergedStr);
}
}
Output
StringJoiner 1 = Asia is a continent and StringJoiner 2 = Tokyo is a city New StringJoiner (strJoin1 + strJoin2) = Asia is a continent and Tokyo is a city
Advertisements
