Code Logo

Welcome Board Line with joining()

Published at22 Apr 2026
Stream API Easy 0 views
Like0

A school entrance board wants to display guest names on one line instead of printing each name separately. The names should appear in their original order, separated by " | ".

Your task is to return one combined string built from the list of names. If the list is empty, the answer should be an empty string.

For example, if the input is ["Ayu", "Bima", "Cici"], the result should be "Ayu | Bima | Cici". If the input is ["Dino"], the result should simply be "Dino".

This challenge focuses on the joining() collector in a Java stream. The goal is not to transform or remove names, but to combine them into one readable output line.

Example Input & Output

Example 1
Input
names = ["Dino"]
Output
"Dino"
Explanation

A one-name list produces a one-name string without extra separators.

Example 2
Input
names = []
Output
""
Explanation

An empty list returns an empty string.

Example 3
Input
names = ["Ayu", "Bima", "Cici"]
Output
"Ayu | Bima | Cici"
Explanation

The names are combined into one line with the required separator.

Algorithm Flow

Recommendation Algorithm Flow for Welcome Board Line with joining()
Recommendation Algorithm Flow for Welcome Board Line with joining()

Solution Approach

This problem is a very natural use case for Collectors.joining(). Instead of building the result manually with loops and separators, Java can collect the stream elements into one string in a single step.

The separator here is fixed: " | ". That means the stream pipeline can stay short and clear:

return names.stream()
    .collect(Collectors.joining(" | "));

This works because the collector handles the separator placement for you. There is no extra trailing delimiter, and a one-item list stays as just that one name. An empty list naturally becomes an empty string.

The runtime is O(n) in the number of names and characters processed, and the code reads very closely to the problem statement: stream the names and join them with the given separator.

Best Answers

java - Approach 1
import java.util.List;
import java.util.stream.Collectors;

class Solution {
    public static String buildWelcomeBoard(List<String> names) {
        return names.stream()
                .map(String::trim)
                .collect(Collectors.joining(" | "));
    }
}