Join a list of strings with '[' as prefix, ']' as suffix, and ',' as delimiter using streams.

Published: (April 4, 2026 at 09:53 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Steps

  1. Convert the list to a stream

    languageList.stream()
  2. Collect the elements into a single string using Collectors.joining with a delimiter, a prefix, and a suffix:

    String result = languageList.stream()
                                .collect(Collectors.joining(",", "[", "]"));

    Collectors.joining(",", "[", "]") joins the elements of the stream into a single String, separated by commas, and adds [ at the beginning and ] at the end.

Result

The code above produces a string like:

[Java,Python,JavaScript]

Visual Example

Joining example

0 views
Back to Blog

Related posts

Read more »

Global Variable VS Local Variable

Global Variable in Java Java does not support true global variables. Instead, it uses class‑level variables, which behave similarly. Types of Class‑Level Varia...

OpenJDK: Panama

Project Panama: Interconnecting JVM and native code We are improving and enriching the connections between the Java virtual machine and well‑defined but “forei...