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