Courseiva
Working with Streams and Lambda ExpressionsmediumMultiple ChoiceObjective-mapped

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

A Java team is processing a large dataset with parallel streams. They notice inconsistent results due to non-atomic operations on shared mutable state. Which approach should they use to ensure thread-safety while maximizing performance?

⚠ Common exam trap

A common mix-up: candidates assume any thread-safe data structure (like `ConcurrentHashMap`) used with `forEach` or `collect` is sufficient, but they overlook that the stream's reduction mechanism must be designed for concurrent accumulation, which only concurrent `Collector` implementations provide correctly.

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 the collect method with a concurrent Collector such as toConcurrentMap().

The `collect` method with a concurrent `Collector` like `toConcurrentMap()` ensures thread-safe accumulation by leveraging `ConcurrentHashMap` internally, which uses fine-grained locking or lock-free operations. This approach allows multiple threads to update the shared mutable state concurrently without external synchronization, maximizing parallelism and performance while maintaining consistency.

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 the collect method with a concurrent Collector such as toConcurrentMap().

    Why this is correct

    Concurrent Collectors are designed for parallel reduction, using internal synchronization and efficient merging.

  • Use forEach with AtomicInteger and update atomically.

    Why it's wrong here

    AtomicInteger works for simple counters, but for complex accumulation, it may not be sufficient and can still cause contention.

  • Use synchronized blocks inside the lambda expression.

    Why it's wrong here

    Synchronization defeats the purpose of parallelism and introduces contention, reducing performance.

  • Use ConcurrentHashMap for accumulation, but collect using toList().

    Why it's wrong here

    ConcurrentHashMap provides thread-safe updates, but collecting into it via toList() is not concurrent; the combiner may cause race conditions.

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.