Quick revision
The short answer
We build it with Collector.of(StringBuilder::new, accumulator, combiner, finisher). The supplier creates the mutable container, a StringBuilder; the accumulator adds one string with a comma and stops growing once 100 characters are reached; the combiner merges two builders for parallel use; and the finisher turns the builder into the result, truncated to 100 characters. Characteristics such as IDENTITY_FINISH, UNORDERED and CONCURRENT tell the framework which optimisations are safe; this collector declares none, and its combiner must be correct even if we only ever run it sequentially.
What to remember
In this answer, we wrote a collector that joins strings with commas up to 100 characters. The supplier creates a StringBuilder, the accumulator appends one element with a comma until the limit is reached, the combiner merges two builders in order for parallel streams, and the finisher truncates to exactly 100 characters. We saw why the combiner must be correct even when we only use sequential streams, and why this collector declares none of the characteristics IDENTITY_FINISH, UNORDERED or CONCURRENT.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We build it with Collector.of(StringBuilder::new, accumulator, combiner, finisher). The supplier creates the mutable container, a StringBuilder; the accumulator adds one string with a comma and stops growing once 100 characters are reached; the combiner merges two builders for parallel use; and the finisher turns the builder into the result, truncated to 100 characters. Characteristics such as IDENTITY_FINISH, UNORDERED and CONCURRENT tell the framework which optimisations are safe; this collector declares none, and its combiner must be correct even if we only ever run it sequentially.
Why this question is asked
Writing a custom collector is the best way to understand how collect() works, because every built-in collector, from toList to groupingBy, is made of the same four functions. It also comes up in real code whenever the built-in collectors almost fit but not quite, as with a joined string that must respect a length limit for a database column or a log line. Interviewers ask this to see whether we know the role of each function and especially the combiner, which most people never test. In this answer, we will recap the Collector contract, look at the simple solution and its weakness, write the custom collector step by step, test the combiner, and explain the characteristics.
1A Quick Recap of the Collector Contract
A Collector<T, A, R> collects elements of type T into a mutable container of type A and produces a result of type R. It is defined by four functions and a set of characteristics. The supplier creates a new, empty container. The accumulator adds one element to a container. The combiner merges two containers into one, which is needed when a parallel stream splits the work and each thread fills its own container. The finisher converts the final container into the result. Collector.of builds a collector from these pieces, so we do not have to implement the interface by hand.
2The Simple Version and Its Weakness
The quickest correct answer uses existing collectors: join everything with a comma, then cut the result down.
This is fine for small inputs, and it is a good thing to mention first. Its weakness is that it builds the whole joined string before truncating. For a stream of a million names, it allocates a string of several megabytes only to keep the first 100 characters. A custom collector can stop growing as soon as it has enough.
3Writing the Custom Collector
We use a StringBuilder as the container. The accumulator only appends while the builder is shorter than 100 characters; after that, the first 100 characters of the final answer are already fixed, so later elements cannot change the result.
Notice each role. The supplier is a method reference to the StringBuilder constructor, so each container starts empty. The accumulator decides whether a comma is needed by checking whether the builder already has content, then appends. The finisher is needed because the last element appended may push the builder beyond 100 characters, so we cut it to exactly 100. One subtlety: because we test sb.length() > 0 to decide on the comma, an empty string as the first element produces no leading comma. If empty elements must be represented faithfully, the container can be a tiny class with a StringBuilder and a boolean flag instead.
4Why the Combiner Matters Even for Sequential Streams
In a sequential stream the combiner is never called, so a broken combiner can sit unnoticed for years. But the collector is a reusable value: anyone can pass it to a parallel stream, and the framework will then split the input, run the supplier and accumulator on each part, and merge the parts with the combiner. The contract requires that the result is the same either way, so we must write and test the combiner as carefully as the accumulator.
6 messages in order. Play them, or step through them one at a time.
The combiner merges left and right in that order, which preserves encounter order, and it handles the edge cases: an empty right side, an empty left side, and a left side that has already reached the limit, in which case the right side cannot affect the first 100 characters and is dropped. A common bug is to forget the comma between the two halves, which would give a,bc,d in parallel.
5What the Characteristics Mean
Characteristics are hints that tell the stream framework which optimisations are safe. IDENTITY_FINISH says the finisher is the identity function, so the container can be returned as the result without calling it. UNORDERED says the result does not depend on the order of the elements, so the framework may process them in any order. CONCURRENT says a single container can be safely updated by several threads at once, so a parallel stream can share one container instead of combining many.
| Characteristic | Promise | Our collector |
|---|---|---|
IDENTITY_FINISH | the container is the result | no: finisher truncates |
| UNORDERED | element order does not matter | no: order is in the text |
| CONCURRENT | one container is safe across threads | no: StringBuilder is not |
Our collector declares none of them. Its finisher really does convert and truncate, so IDENTITY_FINISH would be wrong. Order matters, since "a,b" and "b,a" are different results, so UNORDERED would be wrong. StringBuilder is not thread safe, so CONCURRENT would corrupt the result. Declaring a characteristic that is not true does not cause an error; it silently produces wrong results, which is why the safest default is to declare nothing unless we are sure.
6Follow-up questions
Why is the container type a StringBuilder and not a String?
Because a collector accumulates into a mutable container. Strings are immutable, so appending would create a new String each time and the accumulator, which returns void, would have nowhere to put it. StringBuilder lets the accumulator modify the container in place.
How would you test this collector?
With a few unit tests: empty stream gives an empty string, short input gives the full join, long input gives exactly 100 characters equal to the first 100 of the simple version, and the same inputs through parallelStream give identical results. Comparing against the simple collectingAndThen version is a convenient oracle.
When should a custom collector be a class rather than Collector.of?
When it needs a richer container or will be reused widely and documented, such as a collector that computes several statistics. Collector.of is ideal for small, local collectors; a named class implementing Collector is clearer for shared library code.
7Conclusion
In this answer, we wrote a collector that joins strings with commas up to 100 characters. The supplier creates a StringBuilder, the accumulator appends one element with a comma until the limit is reached, the combiner merges two builders in order for parallel streams, and the finisher truncates to exactly 100 characters. We saw why the combiner must be correct even when we only use sequential streams, and why this collector declares none of the characteristics IDENTITY_FINISH, UNORDERED or CONCURRENT.
Open with
I'd first mention the simple version: collectingAndThen of joining with a comma, then substring to 100. It's correct, but it builds the whole joined string first, which is wasteful for a large stream.
Then, if they want more
So I'd write a custom one with Collector of. The supplier is StringBuilder colon colon new: it creates the mutable container, a fresh one per thread in a parallel stream.
The accumulator adds one element: if the builder is already at 100 characters it returns, because the result is already decided; otherwise it appends a comma if the builder isn't empty, then the string.
The combiner merges two containers for parallel streams: if the left side is already full or the right side is empty, it returns left; otherwise it appends a comma and the right side, which keeps encounter order. The finisher converts the container into the result, cutting it to exactly 100 characters, because the last append might overshoot.
The combiner has to be correct even if we only run sequentially, because anyone can pass this collector to a parallel stream, and the contract says the result must be the same. I'd test it against the simple version with parallel streams.
Characteristics are hints: IDENTITY_FINISH means the container is the result, UNORDERED means order doesn't matter, CONCURRENT means one container can be shared across threads. This collector declares none, since the finisher truncates, order is in the text, and StringBuilder isn't thread safe.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a custom Collector that collects strings into a single comma-separated string with...": https://scrivio-interview-library.netlify.app/java/streams/05764ae78e/