Collectors.joining builds the String directly, which is clearer and faster than reducing with string concatenation.
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);
SELECT id, email, country FROM users
ID|EMAIL|COUNTRY
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