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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

stream.collect(Collectors.joining(delimiter, prefix, suffix))

Parameters

ParameterDescription
delimiter, prefix, suffix optional
CharSequence
Text between, before and after the elements.

Return value

A String.

Examples

Join with separators
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.

Summarise objects
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

VersionChange
Java Java 8Added

Was this page helpful?