计算Map中每个类别的产品总和 - Java

Calculate the product total sum of each category in Map - Java

我有一个地图,我正在尝试为每个类别名称计算产品的总和值,然后获取总和最大的类别,并显示此总和。

我按类别对地图进行了分组,现在我正在努力计算这个总和,有什么解决方案吗?

那是我的 class:

public class ProductCategories {
        private String categoryName;
        private int productId;
        private double price;

        public ProductCategories(String categoryName, int productId, double price) {
            this.categoryName = categoryName;
            this.productId = productId;
            this.price = price;
        }

        public String getCategoryName() {
            return categoryName;
        }

        public int getProductId() {
            return productId;
        }

        public double getPrice() {
        return price;
    }



        public void setCategoryName(String categoryName) {
            this.categoryName = categoryName;
        }

        public void setProductId(int productId) {
            this.productId = productId;
        }

        public void setProductId(double price) {
        this.price = price;
    }


    @Override
        public String toString() {
            return "ProductCategories{" + "Category name='" + categoryName + '\'' + ", productID=" + productId + ", price='" + price + '\'' + '}';
        }


        private static Map<String, Integer> productIdToNameMap(List<ProductCategories> categoryList) {
            return categoryList.stream()
                    .collect(Collectors.toMap(ProductCategories::getCategoryName, ProductCategories::getProductId, (first, second) -> first + second, TreeMap::new
                    ));
        }

        private static Map<String, List<ProductCategories>> multiValueMap(List<ProductCategories> categoryList) {
            return categoryList.stream()
                    .collect(Collectors.groupingBy(ProductCategories::getCategoryName));
        }

        public static void main(String[] args) {
            List<ProductCategories> categoryList = new ArrayList<>();
            categoryList.add(new ProductCategories("The Fellowship of the Ring", 1954, 12));
            categoryList.add(new ProductCategories("The Return of the King", 1954, 12));
            categoryList.add(new ProductCategories("The Return of the King", 1955, 12));


            //System.out.println(productIdToNameMap(categoryList));
            System.out.println(multiValueMap(categoryList));
        }
}

输出:

{The Return of the King=[ProductCategories{Category name='The Return of the King', productID=1954, price='12.0'}, ProductCategories{Category name='The Return of the King', productID=1955, price='12.0'}], The Fellowship of the Ring=[ProductCategories{Category name='The Fellowship of the Ring', productID=1954, price='12.0'}]}

首先,您的方法名称有些误导。你应该考虑一下,改成比multiValueMap.

更有意义的东西

您可以通过简单地应用下游收集器来为各个类别创建总和,例如 Collectors.summingDouble:

private static Map<String, Double> multiValueMap(List<ProductCategories> categoryList) {
    return categoryList.stream()
            .collect(Collectors.groupingBy(ProductCategories::getCategoryName,
                    Collectors.summingDouble(ProductCategories::getPrice)));
}

I am trying to calculate the total sum value of products for each CategoryName, then get the category where the total sum is the greatest, and display this sum.

要找到最高总价,首先您需要找到每个类别的总价。

您可以将收集器 Collectors.groupingByCollectors.summingDouble 的组合用作下游,或者通过 Collectors.toMap 提供 合并函数 作为这个收集器的第三个参数。

然后在中间映射的值上创建一个流并检索最大值。


如果您同时需要最高总价和类别名称,则需要在条目集上创建一个流并查看具有最大值的条目,提供 Map.Entry.comparingByValue() 作为比较器。

    private static Map.Entry<String, Double> getMaxByCategoryName(List<ProductCategories> categoryList) {
        return categoryList.stream()
            .collect(Collectors.toMap(ProductCategories::getCategoryName,
                        ProductCategories::getPrice,
                        Double::sum))
            .entrySet().stream()
            .max(Map.Entry.comparingByValue())
            .orElse(Map.entry("no data evalable", 0d));
    }