Streams: joining with prefix, delimiter and suffix

Collectors.joining builds the String directly, which is clearer and faster than reducing with string concatenation.

Code
var columns = List.of("id", "email", "country");

String sql = columns.stream().collect(Collectors.joining(", ", "SELECT ", " FROM users"));
System.out.println(sql);

String piped = columns.stream().map(String::toUpperCase).collect(Collectors.joining("|"));
System.out.println(piped);
Output
SELECT id, email, country FROM users
ID|EMAIL|COUNTRY
Advertisement

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-07-30