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

Kursinhalt

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

Aufgabe

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).

Lösung

java

solution

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
toggle bottom row

book
Challenge: Build a Custom Collector for Category Counting

Aufgabe

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).

Lösung

java

solution

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
We're sorry to hear that something went wrong. What happened?
some-alt