Java 25 (LTS) reference · Stream API
Java Collectors.joining() Method
Join a stream of strings into one string, with an optional separator, prefix and suffix.
Java 25 (LTS) Since Java 8
Syntax
stream.collect(Collectors.joining(delimiter, prefix, suffix))
Parameters
| Parameter | Description |
|---|---|
delimiter, prefix, suffix optionalCharSequence |
Text between, before and after the elements. |
Return value
A String.
Examples
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
System.out.println(Stream.of("Java", "Kotlin", "Scala").collect(Collectors.joining(", ")));
System.out.println(Stream.of("a", "b").collect(Collectors.joining("|", "[", "]")));
}
}
Output
Java, Kotlin, Scala [a|b]
Run this on your computer — see “Get Started” for setup.
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
record Item(String name, int qty) {}
List<Item> cart = List.of(new Item("mug", 2), new Item("tea", 1));
System.out.println(cart.stream().map(i -> i.qty() + "x " + i.name()).collect(Collectors.joining("; ")));
}
}
Output
2x mug; 1x tea
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 8 | Added |
Related
String.join()· Join strings or a collection of strings into one string, with a separator between them.stream.toList()· Collect a stream's elements into an unmodifiable List. Shorter than collect(Collectors.toList()).- Java Streams · tutorial