Courseiva
Working with Streams and Lambda ExpressionshardMultiple ChoiceObjective-mapped

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

Given a list of strings, which of the following will produce a Map<String, Long> counting the occurrences of each string?

⚠ Common exam trap

Many exam-takers choose `Collectors.toMap` with a constant value (like `v->1`) without realizing it fails on duplicate keys, or they pick `summingInt` thinking it produces a `Long` when it actually produces an `Integer`, missing the exact type constraint of `Map<String, Long>`.

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

.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))

`Collectors.groupingBy(Function.identity(), Collectors.counting())` groups each string by itself (using `Function.identity()` as the classifier) and then applies a downstream collector `Collectors.counting()` that counts the number of elements in each group, producing a `Map<String, Long>` where each key is a unique string and the value is its frequency.

Answer analysis

Option-by-option breakdown

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

  • .collect(Collectors.toMap(Function.identity(), String::length))

    Why it's wrong here

    This maps each string to its length, not counting occurrences.

  • .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))

    Why this is correct

    Correct. groupingBy with counting as downstream collector produces a long count per key.

  • .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)))

    Why it's wrong here

    summingInt returns Map<String, Integer>, not Long.

  • .collect(Collectors.toMap(Function.identity(), v->1))

    Why it's wrong here

    This toMap doesn't handle duplicate keys; it will throw IllegalStateException if duplicates occur.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.