Courseiva
Working with Streams and Lambda ExpressionshardMultiple ChoiceObjective-mapped

1Z0-829 Working with Streams and Lambda Expressions Practice Question

A company runs a Java 17 microservice that processes real-time financial transactions. The application receives a large number of transactions per second, each with a timestamp, amount, and type. The current implementation uses a sequential stream to filter and aggregate transactions into a Map<TransactionType, DoubleSummaryStatistics>. The team observes high latency and CPU spikes during peak loads. They suspect the stream pipeline is inefficient. The pipeline code is:

Map<TransactionType, DoubleSummaryStatistics> stats = transactions.stream() .filter(t -> t.getTimestamp().isAfter(Instant.now().minusSeconds(60))) .collect(Collectors.groupingBy(Transaction::getType, Collectors.summarizingDouble(Transaction::getAmount)));

The transactions list is an ArrayList that is frequently modified by other threads (adding new transactions). The system has multiple CPU cores available. Which of the following changes is the MOST effective way to improve performance while maintaining correctness?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Use .parallelStream() on a snapshot (new ArrayList<>(transactions)) and keep the same collector.

Taking a snapshot via new ArrayList<>(transactions) provides a consistent, immutable view for the stream, reducing contention and allowing parallelStream to leverage multiple cores. However, the copy operation itself is not thread-safe and can throw ConcurrentModificationException if the list is being modified simultaneously. Despite this risk, option B is the most effective among the choices because it attempts to isolate the stream from concurrent modifications and uses a parallel-friendly collector. To guarantee correctness, additional synchronization would be required, but none of the other options offer a better combination of performance and safety.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Use .stream().parallel() with a custom thread pool and ensure the stream source is not modified during operation.

    Why it's wrong here

    Ensuring no modification is difficult in a real-time system; this approach does not address the root cause of concurrent access efficiently.

  • Use .parallelStream() on a snapshot (new ArrayList<>(transactions)) and keep the same collector.

    Why this is correct

    Correct. The snapshot eliminates concurrent modification issues, and parallel processing improves throughput.

  • Replace ArrayList with CopyOnWriteArrayList and use .parallelStream() with the same collector.

    Why it's wrong here

    CopyOnWriteArrayList is inefficient for frequent writes; also, groupingBy is not concurrent and may not perform optimally.

  • Replace the collector with Collectors.groupingByConcurrent() and use .parallelStream() directly on the original list.

    Why it's wrong here

    Although groupingByConcurrent is thread-safe, the source list may be modified during iteration, leading to undefined behavior.

About these practice questions

One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.