Builder: StringJoiner.merge combines two joiners into one

merge() appends another StringJoiner's already-joined content as a single unit into this one, separated by this joiner's own delimiter, rather than re-joining its individual elements. The other joiner's own prefix and suffix are stripped off in the process.

Code
StringJoiner names = new StringJoiner(", ");
names.add("Ann").add("Bo");
StringJoiner cities = new StringJoiner(", ");
cities.add("Rome").add("Oslo");
names.merge(cities);
System.out.println(names);
Output
Ann, Bo, Rome, Oslo
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms