我有一個地圖,我正在嘗試計算每個 CategoryName 的產品總和值,然后獲取總和最大的類別,并顯示這個總和。
我按類別對地圖進行了分組,現在我正在努力如何計算這個總和,有什么解決方案嗎?
那是我的課:
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'}]}
uj5u.com熱心網友回復:
我試圖計算每個產品的總和值
CategoryName,然后得到總和最大的類別,并顯示這個總和。
要找到最高總價,首先您需要找到每個類別的總價。
您可以通過使用收集器的組合Collectors.groupingBy和Collectors.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));
}
uj5u.com熱心網友回復:
首先,您的方法名稱有些誤導。您應該考慮一下并將其更改為比multiValueMap.
您可以通過簡單地應用下游收集器來創建各個類別的總和,例如Collectors.summingDouble:
private static Map<String, Double> multiValueMap(List<ProductCategories> categoryList) {
return categoryList.stream()
.collect(Collectors.groupingBy(ProductCategories::getCategoryName,
Collectors.summingDouble(ProductCategories::getPrice)));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463381.html
