Scrivio Interview library
DebuggingFoundations5 min read · updated 12 November 2026

A developer writes list.stream().map(this::save); in a service method and no records are saved. Explain and fix it.

Nothing is saved because map is an intermediate operation: list.stream().map(this::save) only builds a pipeline, and without a terminal operation the mapper is never called. For side effects like saving, use list.forEach(this::save) or a for loop; if we need the saved results, end with toList() or collect. Using map for side effects is a misuse even with a terminal operation, because some terminals may skip the mapper entirely, for example count() when the size is known.

Why this question is asked

This bug is common, and it is quiet: the code compiles, the method returns normally, tests that only check for exceptions pass, and the records simply never reach the database. Interviewers like it because the fix is one word, but the explanation shows whether we understand how streams run and why map is the wrong tool for actions. In this answer, we will see why the line does nothing, fix it in the two common ways, look at the case where we need the saved entities back, and then see why map with a side effect is fragile even when there is a terminal operation.

1Why Nothing Happens

A stream pipeline has a source, intermediate operations and a terminal operation. Intermediate operations such as map, filter and sorted are lazy: calling them only records a stage and returns a new stream. The terminal operation, such as forEach, toList, collect or count, is what pulls elements through the stages.

The statement in the question has a source and one intermediate operation, and then the line ends. The new Stream object is returned, ignored, and later garbage collected. No element was ever pulled, so this::save was never called.

Java
public void saveAll(List<Order> list) {
list.stream().map(this::save); // builds a pipeline and throws it away
}
saveAll(List.of(o1, o2, o3));
// no SQL statements, no exception, nothing saved

Notice that the compiler does not warn about this by default, because ignoring a method's return value is legal Java. Static analysis tools such as Error Prone or SonarQube do flag an unused stream, which is a good reason to run them in the build.

2Fix 1: forEach or a Plain Loop for Side Effects

Saving is a side effect: it changes something outside the stream, and we do not need a transformed value back. The right operations for that are forEach, or simply a loop.

Java
list.forEach(this::save); // Iterable.forEach, no stream needed
for (Order o : list) { // clearer when we need try/catch or early exit
save(o);
}

Both call save once per element, in order. We do not even need stream() here: List has its own forEach. The loop is the better choice if the body grows, if we need to handle exceptions per element, or if we want to stop early.

3Fix 2: A Terminal Operation When We Need the Results

Sometimes save returns something useful, such as the entity with its generated id, and we really do want a list of results. Then map is the right operation, because we are transforming each order into its saved version, and we end the pipeline with toList().

Java
List<Order> saved = list.stream()
.map(this::save) // now runs once per element
.toList(); // terminal operation pulls every element
saved.get(0).id(); // 1001, the generated id

In a Spring Data application the cleanest version is often repository.saveAll(list), which saves the whole batch and returns the saved entities, and which can use JDBC batching when it is configured.

4Why map With Side Effects Is Fragile Even With a Terminal

Adding a terminal operation makes the bug go away in most cases, but it does not make map with side effects correct. The stream library is allowed to skip calling a mapper when the result does not depend on it, and some terminals use that freedom.

Java
long n = list.stream().map(this::save).count();
System.out.println(n); // 3
// on Java 9 and later: save was never called, nothing was saved
Step throughDoes count() run the mapper?
  1. list.stream().map(save).count()
  2. Is the size known from the source?
    YesCheck the stagesNoTraverse and count
  3. Can any stage change the number of elements?
    YesTraverse and countNoReturn size(): save never runs
  4. filter, flatMap and similar change itmap and peek do not

4 steps in order. Play them, or step through them one at a time.

count() only needs the number of elements. The source is a List, which knows its size, and map never changes how many elements there are, so since Java 9 the implementation returns the list's size directly without running the pipeline. The documentation of count() says explicitly that an implementation may choose not to execute the pipeline if it can compute the count directly from the source. The same applies to peek. So the rule is simple: map is for transforming values; anything that must happen for every element belongs in forEach or a loop.

5Follow-up questions

Why can stream().map(...).count() skip calling the mapper in recent JDKs?

Because the source reports its size and map keeps the size unchanged. A List's spliterator has the SIZED characteristic, which becomes a flag on the stream, and map preserves that flag while filter clears it. Since Java 9, count() checks the flag and, if the size is known, returns it without traversing. The mapper's side effects are simply not needed to compute the answer, and the specification of count() allows exactly this.

Is forEach on a parallel stream safe for saving?

It runs save on several threads in no particular order, so save must be thread safe and each call needs its own transaction or connection handling. With Spring, a transaction bound to the calling thread does not extend to the worker threads. For database writes, a sequential loop or saveAll is almost always the better choice.

Would peek(this::save).toList() be a good alternative?

No. peek exists mainly for debugging, and like map it may be skipped when the terminal does not need the elements. Using it for required side effects has the same fragility as map.

6Conclusion

In this answer, we saw that list.stream().map(this::save) saves nothing because map is lazy and no terminal operation ever pulls the elements. For side effects we use list.forEach(this::save) or a loop; when we need the saved results we use map with toList(), or saveAll in Spring Data. We also saw that map with side effects stays fragile with a terminal operation, since count() on a sized source can return the size without calling the mapper at all.

What to say in the interviewAbout 90 seconds aloud

Open with

The problem is that map is an intermediate operation, and intermediate operations are lazy. list stream map this::save just builds a pipeline and returns a new stream, and since the statement ends there, the stream is thrown away without any element being pulled through.

Then, if they want more

So save is never called, there's no error, and nothing reaches the database. The fix depends on what we want. If we just want to save each order, that's a side effect, so I'd use list forEach this::save, or a plain for loop if I need per-element error handling.

If we need the saved entities back, for example to get generated ids, then map is correct, but it has to end with a terminal operation like toList. In Spring Data I'd probably just call repository saveAll. I'd also point out that map for side effects is wrong even with a terminal operation.

For example, list stream map save count on Java 9 or later returns 3 without calling save at all, because the list knows its size, map can't change the number of elements, and count is allowed to return the size directly without running the pipeline.

So the rule is: map is for transforming values, forEach or a loop is for actions that must happen for every element. And I'd add a static analysis rule, like Error Prone's, that flags unused streams.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
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.
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 "A developer writes list.stream().map(this::save); in a service method and no records are...": https://scrivio-interview-library.netlify.app/java/streams/fb71227ec7/