You can actually use Collectors.joining(CharSequence delimiter,CharSequence prefix,CharSequence suffix) and you can look here for the API.

String values = list.stream().collect(Collectors.joining("','", "'", "'"));
Answer from Vasu on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › StringJoiner.html
StringJoiner (Java Platform SE 8 )
July 21, 2026 - This can be used, for example, when creating a string using set notation to indicate an empty set, i.e. "{}", where the prefix is "{", the suffix is "}" and nothing has been added to the StringJoiner. ... StringJoiner sj = new StringJoiner(":", "[", "]"); sj.add("George").add("Sally").add("Fred"); String desiredString = sj.toString(); A StringJoiner may be employed to create formatted output from a Stream using Collectors.joining(CharSequence).
🌐
Medium
medium.com › @AlexanderObregon › javas-collectors-joining-method-explained-a4adb120b785
Java’s Collectors.joining() Method Explained | Medium
November 2, 2024 - Learn how to use Java's Collectors.joining() method to concatenate stream elements into a formatted string. Includes examples for user names, file paths, and URLs.
🌐
Baeldung
baeldung.com › home › java › java string › java stringjoiner
Java StringJoiner | Baeldung
August 30, 2024 - StringJoiner is a new class added in Java 8 under java.util package. Simply put, it can be used for joining Strings making use of a delimiter, prefix, and suffix. ... @Test public void whenAddingElements_thenJoinedElements() { StringJoiner joiner ...
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › join or append stream elements in java
Join or Append Stream Elements in Java
December 13, 2024 - Stream<String> stream = Stream.of("Hello", "World", "Java", "Streams"); // Joining with no delimiter String result = stream.collect(Collectors.joining()); System.out.println("Joined String: " + result); // Output: HelloWorldJavaStreams // Joining with specified delimiter String result = stream.collect(Collectors.joining(", ")); System.out.println("Joined String: " + result); // Output: Hello, World, Java, Streams // Joining with specified delimiter, prefix and suffix String result = stream.collect(Collectors.joining(", ", "[", "]")); System.out.println("Joined String: " + result); // Output: [Hello, World, Java, Streams] ... Streams may contain null values, which can cause issues during concatenation. Use filter() to remove nulls before joining.
🌐
YouTube
youtube.com › watch
Top 50+ Java Streams - Join the strings with ‘[‘ as prefix, ‘]’ as suffix and ‘,’ as delimiter? - YouTube
Join a List of Strings with Custom Prefix, Suffix, and Delimiter Using Java Streams | Java 8 Stream API Tutorial 🧩In this video, you'll learn how to join...
Published: May 22, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-8-streams-collectors-joining-method-with-examples
Java 8 Streams | Collectors.joining() method with Examples - GeeksforGeeks
January 22, 2026 - import java.util.Arrays; import ... Collectors.joining(", ", "[", "]") joins the elements using ", " as the delimiter, "[" as the prefix, and "]" as the suffix....
Find elsewhere
🌐
DZone
dzone.com › coding › java › concatenating strings in java 8
Concatenating Strings in Java 8
August 8, 2017 - If we want to use StringJoiner ... have to use it directly or wrapped by another class. People experienced with Java 8 Streams most probably know where we're going with this: the Collectors class: public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return new CollectorImpl<>( () -> new StringJoiner(delimiter, prefix, suffix), StringJoiner::add, ...
🌐
Stack Overflow
stackoverflow.com › questions › 72633982 › how-to-add-and-modify-prefix-in-java-using-string-arrays
How to add and modify prefix in Java, using String arrays? - Stack Overflow
June 15, 2022 - Create the result array, loop through the original names by index and assign the prefix + space + current original name to the corresponding result array slot. public static String[] addPrefix(String[] names, String prefix) { String[] result ...
🌐
Javaprogramto
javaprogramto.com › 2020 › 12 › java-8-stream-join-string.html
Java 8 Streams - Join or Append Stream of Strings using Collectors.joining() JavaProgramTo.com
November 26, 2021 - Collectors.joining() method does concatenate the list of values or list of strings or stream of values or strings into a new string. For this method, we can pass the our own delimiter and and also we can supply the prefix, suffix to the output ...
Top answer
1 of 3
8

Yes, this is possible using a custom Collector instance that will use an anonymous object with a count of items in the stream and an overloaded toString() method:

public String format(Stream<String> stream) {
    return stream.collect(
            () -> new Object() {
                StringJoiner stringJoiner = new StringJoiner(",");
                int count;

                @Override
                public String toString() {
                    return count == 1 ? stringJoiner.toString() : "[" + stringJoiner + "]";
                }
            },
            (container, currentString) -> {
                container.stringJoiner.add(currentString);
                container.count++;
            },
            (accumulatingContainer, currentContainer) -> {
                accumulatingContainer.stringJoiner.merge(currentContainer.stringJoiner);
                accumulatingContainer.count += currentContainer.count;
            }
                         ).toString();
}

Explanation

Collector interface has the following methods:

public interface Collector<T,A,R> {
    Supplier<A> supplier();
    BiConsumer<A,T> accumulator();
    BinaryOperator<A> combiner();
    Function<A,R> finisher();
    Set<Characteristics> characteristics();
}

I will omit the last method as it is not relevant for this example.

There is a collect() method with the following signature:

<R> R collect(Supplier<R> supplier,
              BiConsumer<R, ? super T> accumulator,
              BiConsumer<R, R> combiner);

and in our case it would resolve to:

<Object> Object collect(Supplier<Object> supplier,
              BiConsumer<Object, ? super String> accumulator,
              BiConsumer<Object, Object> combiner);
  • In the supplier, we are using an instance of StringJoiner (basically the same thing that Collectors.joining() is using).
  • In the accumulator, we are using StringJoiner::add() but we increment the count as well
  • In the combiner, we are using StringJoiner::merge() and add the count to the accumulator
  • Before returning from format() function, we need to call toString() method to wrap our accumulated StringJoiner instance in [] (or leave it as is is, in case of a single-element stream

The case for an empty case could also be added, I left it out in order not to make this collector more complicated.

2 of 3
2

There is already an accepted answer and I upvoted it too.

Still I would like to offer potentially another solution. Potentially because it has one requirement:
The stream.spliterator() of Stream<String> stream needs to be Spliterator.SIZED.

If that applies to your case, you could use also this solution:

  public String format(Stream<String> stream) {
    Spliterator<String> spliterator = stream.spliterator();
    StringJoiner sj = spliterator.getExactSizeIfKnown() == 1 ?
      new StringJoiner("") :
      new StringJoiner(",", "[", "]");
    spliterator.forEachRemaining(sj::add);

    return sj.toString();
  }

According to the JavaDoc Spliterator.getExactSizeIfKnown() "returns estimateSize() if this Spliterator is SIZED, else -1." If a Spliterator is SIZED then "estimateSize() prior to traversal or splitting represents a finite size that, in the absence of structural source modification, represents an exact count of the number of elements that would be encountered by a complete traversal."

Since "most Spliterators for Collections, that cover all elements of a Collection report this characteristic" (API Note in JavaDoc of SIZED) this could be the desired directer way.

EDIT:
If the Stream is empty we can return an empty String at once. If the Stream has only one String there is no need to create a StringJoiner and to copy the String to it. We return the single String directly.

  public String format(Stream<String> stream) {
    Spliterator<String> spliterator = stream.spliterator();

    if (spliterator.getExactSizeIfKnown() == 0) {
      return "";
    }

    if (spliterator.getExactSizeIfKnown() == 1) {
      AtomicReference<String> result = new AtomicReference<String>();
      spliterator.tryAdvance(result::set);
      return result.get();
    }

    StringJoiner result = new StringJoiner(",", "[", "]");
    spliterator.forEachRemaining(result::add);
    return result.toString();
  }
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-8-stringjoiner
Java 8 StringJoiner with example
September 11, 2022 - In java 8, a new class StringJoiner is introduced in the java.util package. Using this class we can join more than one strings with the specified delimiter, we can also provide prefix and suffix to the final string while joining multiple strings.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › StringJoiner.html
StringJoiner (Java SE 11 & JDK 11 )
January 20, 2026 - If no characters are added to the StringJoiner and methods accessing the string value of it are invoked, it will return the prefix + suffix (or properties thereof) in the result, unless setEmptyValue has first been called.
🌐
YouTube
youtube.com › naveen automationlabs
Java Streams Interview Question - 05 - Print Numbers Starts With Prefix 2 using Streams - YouTube
Java Streams Interview Question - 05 - Print Even & Odd Numbers using StreamsSchedule a meeting in case of any queries/guidance/counselling:https://calendly....
Published: December 8, 2021
Views: 19K
🌐
Baeldung
baeldung.com › home › java › java streams › string operations with java streams
String Operations with Java and Stream API | Baeldung
January 8, 2024 - Java 8 has introduced a new Stream API that lets us process data in a declarative manner. In this quick article, we would learn how to use the Stream API to split a comma-separated String into a list of Strings and how to join a String array into a comma-separated String.
🌐
DEV Community
dev.to › prk0906 › join-a-list-of-strings-with-as-prefix-as-suffix-and-as-delimiter-using-streams-1lc3
Join a list of strings with '[' as prefix, ']' as suffix, and ',' as delimiter using streams. - DEV Community
April 4, 2026 - Report Abuse · Praven Kumar · Posted on Apr 4 · #java #java8 #streams · Step 1: Convert List → Stream -> languageList.stream() Step 2: Collecting using joining() Collectors.joining(delimiter, prefix, suffix)->.collect(Collectors.joining(",", ...
🌐
Medium
neesri.medium.com › understanding-java-8-stringjoiner-string-join-and-collectors-joining-5d3b8cdd1551
Understanding Java 8 StringJoiner, String.join(), and ...
January 3, 2025 - ... StringJoiner(CharSequence delimiter) This creates a StringJoiner that joins strings using the specified delimiter. Example: new StringJoiner(", ") StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) This adds a ...