Courseiva
Working with Streams and Lambda ExpressionsmediumMultiple ChoiceObjective-mapped

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

A company uses a large dataset of customer orders. They want to compute statistics: total orders, average amount, and maximum amount per city. They write: Map<String, IntSummaryStatistics> stats = orders.stream() .collect(Collectors.groupingBy(Order::getCity, Collectors.summarizingInt(Order::getAmount))); The code works but is slower than expected when run on a large dataset. They suspect the grouping operation is not taking advantage of parallelism. They want to improve performance by making the collector concurrent. Which change is correct?

⚠ Common exam trap

It's easy for candidates to think `parallelStream()` alone is sufficient for concurrent grouping, but they overlook that the collector itself must be designed for concurrent accumulation to avoid contention and ensure thread safety.

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 Collectors.groupingByConcurrent(Order::getCity, Collectors.summarizingInt(Order::getAmount)).

`Collectors.groupingByConcurrent` performs an unordered, concurrent reduction that can leverage a `ConcurrentHashMap` internally, allowing the collector to work efficiently with a parallel stream. This reduces contention and improves performance on large datasets compared to the sequential `groupingBy`.

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 Collectors.groupingByConcurrent(Order::getCity, Collectors.summarizingInt(Order::getAmount)).

    Why this is correct

    Correct. `Collectors.groupingByConcurrent` performs a concurrent reduction, using a `ConcurrentHashMap` internally, which allows efficient parallel processing with `parallelStream()`, reducing contention.

  • Use parallelStream() instead of stream().

    Why it's wrong here

    Incorrect. Using `parallelStream()` with a sequential collector like `groupingBy` still causes contention because the collector is not designed for concurrent accumulation; the internal merge step becomes a bottleneck.

  • Keep the code but supply a ConcurrentHashMap as the map supplier to groupingBy.

    Why it's wrong here

    Incorrect. Although `groupingBy` has an overload that accepts a map supplier, supplying a `ConcurrentHashMap` does not make the reduction concurrent. The collector still performs a sequential merge, causing thread contention and overhead.

  • Replace collect with forEach and manually update a ConcurrentHashMap.

    Why it's wrong here

    Incorrect. Manually updating a `ConcurrentHashMap` inside `forEach` is error‑prone and loses the optimized parallel reduction provided by `groupingByConcurrent`, which also handles ordering and merging efficiently.

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.