Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Build a Custom Collector for Category Counting | Terminal Operations in the Stream API
Stream API

book
Challenge: Build a Custom Collector for Category Counting

Завдання

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

Рішення

java

solution

package com.example;

import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
List<Map<String, String>> products = List.of(
Map.of("name", "Laptop", "category", "Electronics"),
Map.of("name", "Phone", "category", "Electronics"),
Map.of("name", "Refrigerator", "category", "Home Appliances"),
Map.of("name", "Microwave", "category", "Home Appliances"),
Map.of("name", "Keyboard", "category", "Electronics")
);

// Using the custom `Collector`
Map<String, Integer> categoryCount = products.parallelStream()
.collect(new CategoryCountCollector());

System.out.println(categoryCount);
}
}

class CategoryCountCollector implements Collector<Map<String, String>, Map<String, Integer>, Map<String, Integer>> {

@Override
public Supplier<Map<String, Integer>> supplier() {
return HashMap::new;
}

@Override
public BiConsumer<Map<String, Integer>, Map<String, String>> accumulator() {
return (map, product) -> map.merge(product.get("category"), 1, Integer::sum);
}
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
package com.example;

import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
List<Map<String, String>> products = List.of(
Map.of("name", "Laptop", "category", "Electronics"),
Map.of("name", "Phone", "category", "Electronics"),
Map.of("name", "Refrigerator", "category", "Home Appliances"),
Map.of("name", "Microwave", "category", "Home Appliances"),
Map.of("name", "Keyboard", "category", "Electronics")
);

// Using the custom `Collector`
Map<String, Integer> categoryCount = products.parallelStream()
.collect(___);

System.out.println(categoryCount);
}
}

class CategoryCountCollector implements Collector<Map<String, String>, Map<String, Integer>, Map<String, Integer>> {

@Override
public Supplier<Map<String, Integer>> supplier() {
return ___;
}

@Override
public BiConsumer<Map<String, Integer>, Map<String, String>> accumulator() {
return (map, product) -> map.merge(product.___(___), ___, Integer::sum);
}

@Override
public BinaryOperator<Map<String, Integer>> combiner() {
return (map1, map2) -> {
map2.forEach((key, value) -> map1.merge(___, ___, Integer::sum));
return map1;
};
}

@Override
public Function<Map<String, Integer>, Map<String, Integer>> finisher() {
return map -> ___; // Simply return the collected data
}

@Override
public Set<Characteristics> characteristics() {
return Set.of(___);
}
}
toggle bottom row
some-alt