Counting Word Occurrences with groupingBy and counting
Exhibit
List<String> items = List.of("apple", "banana", "apple", "orange", "banana", "apple");
Map<String, Long> countMap = items.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(countMap);Refer to the exhibit. What is the output?
Quick Answer
The answer is {apple=3, banana=2, orange=1}. This output results from using the `groupingBy` collector with a downstream `counting` collector on a stream of words, which groups each distinct word and counts its occurrences in the stream. The `groupingBy` classifier function maps each word to itself as the key, while `counting()` tallies the number of times each key appears, producing a `Map<String, Long>`. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this pattern tests your understanding of the `Collectors` utility class and how downstream collectors compose to transform stream data. A common trap is forgetting that `groupingBy` with `counting` returns a `Long` value, not an `Integer`, so watch for type mismatches in exam code. Another pitfall is confusing `groupingBy` with `toMap`—remember that `groupingBy` always groups by a classifier, while `toMap` requires explicit key and value mappers. Memory tip: think of `groupingBy` as sorting items into labeled buckets, then `counting` as tallying each bucket’s contents.
⚠ Common exam trap
A common pitfall is to think that groupingBy always produces a Map<K, List<V>>, but when paired with a downstream collector like counting(), it produces a Map<K, Long> (or Map<K, Integer> with summingInt). Here, candidates might expect option B, which shows a grouped list, but the correct output is a frequency map like option C.
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
✓
{apple=3, banana=2, orange=1}
The code processes a list containing the strings "apple", "banana", "orange", "apple", "banana", "apple" (or an equivalent stream) and uses `Collectors.groupingBy(Function.identity(), Collectors.counting())` to create a frequency map. The `groupingBy` collector groups the elements by their identity (the string itself) and the downstream `counting()` collector counts the occurrences in each group, resulting in a map where the key is the string and the value is the count: apple appears 3 times, banana 2 times, and orange 1 time.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
{apple=2, banana=2, orange=2}
Why it's wrong here
Incorrect.
- ✗
{1=[apple, banana, orange], 2=[apple, banana], 3=[apple]}
Why it's wrong here
Incorrect; that is grouping by count, not counting.
- ✓
{apple=3, banana=2, orange=1}
Why this is correct
Correct.
- ✗
{apple=3, banana=1, orange=1}
Why it's wrong here
Incorrect; banana appears twice.
Go deeper
Related to this question
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 →
Same concept, more angles
1 more way this is tested on 1Z0-829
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Given a list of strings, which of the following will produce a Map<String, Long> counting the occurrences of each string?
hard- A..collect(Collectors.toMap(Function.identity(), String::length))
- ✓ B..collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
- C..collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)))
- D..collect(Collectors.toMap(Function.identity(), v->1))
Why B: `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.
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.