시작하기무료로 시작하기

Grouping by standardized categories

Now that you have standardized the categories of your electronics inventory, you would like to compute the quantity of each category. Merge the quantities into category sums.

이 연습은 강의의 일부입니다

Cleaning Data in Java

강의 보기

연습 안내

  • Define an enum map to store the quantity of each ProductCategory in stockByCategory.
  • Merge the quantities into category sums in stockByCategory.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

public class ElectronicsCategoryExample {
    public enum ProductCategory {
        ELECTRONICS,
        HOME_APPLIANCES,
        UNCATEGORIZED;
    }

    public static void main(String[] args) {
    	Map categoryMap = new HashMap<>();
        categoryMap.put("Home Appliances", ProductCategory.HOME_APPLIANCES);
        categoryMap.put("home appliances", ProductCategory.HOME_APPLIANCES);
        categoryMap.put("HomeAppliances", ProductCategory.HOME_APPLIANCES);
        categoryMap.put("Electronics", ProductCategory.ELECTRONICS);
        categoryMap.put("electronics", ProductCategory.ELECTRONICS);
        categoryMap.put("Electronic", ProductCategory.ELECTRONICS);
        
        Map rawData = Map.of(
                "Home Appliances", 50,
                "home appliances", 25,
                "HomeAppliances", 15,
                "Electronics", 30,
                "electronics", 20,
                "Electronic", 10);

        // Define an enum map to store the quantity of each ProductCategory
        Map stockByCategory = new ____<>(____.class);

        // Merge the quantities into category sums
        rawData.forEach((raw, quantity) -> ____.____(categoryMap.get(raw), quantity, Integer::sum));

		System.out.println("Stock by category:");
        System.out.println(stockByCategory);
    }
}
코드 편집 및 실행