Builder: StringJoiner.add joins parts with delimiter and wrapping

StringJoiner keeps track of the delimiter, prefix and suffix internally, so each add() call only supplies the next piece and the separators are placed automatically. The prefix and suffix appear exactly once, wrapping the whole joined sequence.

Code
StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.add("apple").add("banana").add("cherry");
System.out.println(joiner);
Output
[apple, banana, cherry]
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