Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Build a Custom Collector for Category Counting | Terminal Operations in the Stream API
Stream API
course content

Course Content

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
Challenge: Build a Custom Collector for Category Counting

Task

Swipe to start coding

You need to implement a custom Collector that processes a list of products and counts how many products belong to each category.

  1. In the supplier() method, return a new HashMap.
  2. In the accumulator() method, call the get() method on the product map to retrieve the value of the "category" key and pass it as the first parameter of the merge() method.
  3. For the second parameter in the same method, provide the initial value (1).
  4. In the combiner() method, pass the key of map2 as the first parameter of the merge() method.
  5. For the second parameter in the same method, pass the value of the map2.
  6. In the finisher() method, return the map without modifying it using a lambda.
  7. In the characteristics() method, return a set that ensures the collection remains unchanged (IDENTITY_FINISH).
  8. In the collect() method, pass the implementation of the Collector interface (CategoryCountCollector).

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

book
Challenge: Build a Custom Collector for Category Counting

Task

Swipe to start coding

You need to implement a custom Collector that processes a list of products and counts how many products belong to each category.

  1. In the supplier() method, return a new HashMap.
  2. In the accumulator() method, call the get() method on the product map to retrieve the value of the "category" key and pass it as the first parameter of the merge() method.
  3. For the second parameter in the same method, provide the initial value (1).
  4. In the combiner() method, pass the key of map2 as the first parameter of the merge() method.
  5. For the second parameter in the same method, pass the value of the map2.
  6. In the finisher() method, return the map without modifying it using a lambda.
  7. In the characteristics() method, return a set that ensures the collection remains unchanged (IDENTITY_FINISH).
  8. In the collect() method, pass the implementation of the Collector interface (CategoryCountCollector).

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt