Courseiva
Working with Streams and Lambda ExpressionshardMultiple ChoiceObjective-mapped

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

A company runs a financial application that processes a stream of millions of transaction records daily. Each record is a 'Transaction' object with fields: id, amount, currency, timestamp. The system currently uses a parallel stream to group transactions by currency and compute the sum of amounts per currency, using the following code: Map<String, Double> result = transactions.parallelStream() .collect(Collectors.groupingBy(Transaction::getCurrency, Collectors.summingDouble(Transaction::getAmount))); Recently, performance has degraded significantly. Analysis shows that the stream source is a LinkedList, and the operation involves a large number of distinct currencies (over 1000). The JVM is running on a machine with 4 cores. Which is the best course of action to improve performance?

⚠ Common exam trap

It's easy for candidates to assume the problem is the stream source (LinkedList) or parallelism level, but the real issue is the collector's merge overhead with many distinct keys, which is a subtle but critical performance detail in parallel stream operations.

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

Replace groupingBy with a custom concurrent collector using ConcurrentHashMap.

The performance degradation stems from the parallel stream using a shared `ConcurrentHashMap` internally for the `groupingBy` collector, which incurs significant overhead when merging partial results from many threads, especially with over 1000 distinct currencies. A custom concurrent collector using `ConcurrentHashMap` directly eliminates this merge overhead by allowing threads to update the map concurrently without synchronization bottlenecks, improving throughput on a 4-core machine.

Answer analysis

Option-by-option breakdown

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

  • Change the stream source to an ArrayList and use sequential stream.

    Why it's wrong here

    Changing to ArrayList and sequential stream would not improve performance; the bottleneck is the collector's merge overhead, not the stream source. Sequential stream would be slower for large data.

  • Use a custom thread pool with ForkJoinPool to control parallelism.

    Why it's wrong here

    Using a custom thread pool does not address the merge overhead; the default common ForkJoinPool is already appropriate.

  • Increase the parallelism level to 8.

    Why it's wrong here

    Increasing parallelism level to 8 could worsen performance due to increased contention and merge overhead with many distinct keys.

  • Replace groupingBy with a custom concurrent collector using ConcurrentHashMap.

    Why this is correct

    Replacing groupingBy with a custom concurrent collector using ConcurrentHashMap eliminates the merge step, allowing concurrent updates without synchronization, which improves throughput.

About these practice questions

Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.