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

Contenu du cours

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

Tâche

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 desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2
toggle bottom row

book
Challenge: Build a Custom Collector for Category Counting

Tâche

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 desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
We're sorry to hear that something went wrong. What happened?
some-alt