Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在对相似元素求和后获得列表_Java_List_Arraylist_Collections - Fatal编程技术网

Java 如何在对相似元素求和后获得列表

Java 如何在对相似元素求和后获得列表,java,list,arraylist,collections,Java,List,Arraylist,Collections,我有一个具有以下属性的产品类别列表: public class Products{ private int month; private int year; private int sku; private String detail; private Double quantity; private Double price; private Double discount; private Double %discount; // getters and setters stat

我有一个具有以下属性的产品类别列表:

public class Products{
private int month;
private int year;
private int sku;
private String detail;
private Double quantity;
private Double price;
private Double discount;
private Double %discount;

    // getters and setters

static List<Products> sumProduct(List<Products> itemList) {
    
List<Product> tempListP = new ArrayList<Product>();

    Collections.sort(itemList, Comparator.comparing(Product::getYear)
            .thenComparing(Product::getMonth).thenComparing(Product::getSku));
    
    
    Integer itemCode = null; 
    double quantiti = 0;
    double price = 0;
    double discount = 0;
    double %discount = 0;
    Products itemObj = null;
    Integer itemMonth= null;
    Integer itemYear= null;
    
        for (int i = 0; i < itemList.size(); i++) {
            if (itemCode == null || (itemCode.equals(itemList.get(i).getSku())  && itemMonth.equals(itemList.get(i).getMonth()) && itemMonth.equals(itemList.get(i).getYear())  )) {
                quantiti = quantiti + itemList.get(i).getquantiti();
                price = price + itemList.get(i).getprice();
                discount = discount + itemList.get(i).getdiscount();
                %discount = %discount + itemList.get(i).get%discount();
                itemMonth = itemList.get(i).getMonth();
                itemYea = itemList.get(i).getYear();
            } else {
                
                itemObj = new Product( itemList.get(i).getMonth(), itemList.get(i).getYear(), itemCode, "" , quantiti, price,discount,%discount);
                
                
                if (tempListP.contains(itemObj)) {
                    tempListP.remove(itemObj);
                }
                tempListP.add(itemObj);
                quantiti = 0;
                quantiti = quantiti + itemList.get(i).getquantiti();
                price = 0;
                price = price + itemList.get(i).getprice();
                discount = 0;
                discount = discount + itemList.get(i).getdiscount();
                %discount = 0;
                %discount = %discount + itemList.get(i).get%discount();
                itemMonth = itemList.get(i).getMonth();
                itemYea = itemList.get(i).getYear();
            }

            itemCode = itemList.get(i).getSku();

            if (i == itemList.size() - 1) {
                itemObj = new Product( itemList.get(i).getMonth(), itemList.get(i).getYear(), itemCode, "" , quantiti, price,discount,%discount);
                tempListP.add(itemObj);
            }
        
        
    }
    

    return itemList;
}


}
Month | Year | Sku | Detail | Quantity | Price | Discount | % Discount

7 | 2020 | 50607080 | Tshirt | 4 | 40 | 4 | 0.4

8 | 2020 | 10203040 | Tshirt | 6 | 60 | 6 | 0.6

9 | 2020 | 10203040 | Tshirt | 2 | 20 | 2 | 0.2

3 | 2021 | 10203040 | Tshirt | 3 | 30 | 3 | 0.3

8 | 2021 | 10203040 | Tshirt | 8 | 80 | 8 | 0.8
最终结果应该是:

输出:

public class Products{
private int month;
private int year;
private int sku;
private String detail;
private Double quantity;
private Double price;
private Double discount;
private Double %discount;

    // getters and setters

static List<Products> sumProduct(List<Products> itemList) {
    
List<Product> tempListP = new ArrayList<Product>();

    Collections.sort(itemList, Comparator.comparing(Product::getYear)
            .thenComparing(Product::getMonth).thenComparing(Product::getSku));
    
    
    Integer itemCode = null; 
    double quantiti = 0;
    double price = 0;
    double discount = 0;
    double %discount = 0;
    Products itemObj = null;
    Integer itemMonth= null;
    Integer itemYear= null;
    
        for (int i = 0; i < itemList.size(); i++) {
            if (itemCode == null || (itemCode.equals(itemList.get(i).getSku())  && itemMonth.equals(itemList.get(i).getMonth()) && itemMonth.equals(itemList.get(i).getYear())  )) {
                quantiti = quantiti + itemList.get(i).getquantiti();
                price = price + itemList.get(i).getprice();
                discount = discount + itemList.get(i).getdiscount();
                %discount = %discount + itemList.get(i).get%discount();
                itemMonth = itemList.get(i).getMonth();
                itemYea = itemList.get(i).getYear();
            } else {
                
                itemObj = new Product( itemList.get(i).getMonth(), itemList.get(i).getYear(), itemCode, "" , quantiti, price,discount,%discount);
                
                
                if (tempListP.contains(itemObj)) {
                    tempListP.remove(itemObj);
                }
                tempListP.add(itemObj);
                quantiti = 0;
                quantiti = quantiti + itemList.get(i).getquantiti();
                price = 0;
                price = price + itemList.get(i).getprice();
                discount = 0;
                discount = discount + itemList.get(i).getdiscount();
                %discount = 0;
                %discount = %discount + itemList.get(i).get%discount();
                itemMonth = itemList.get(i).getMonth();
                itemYea = itemList.get(i).getYear();
            }

            itemCode = itemList.get(i).getSku();

            if (i == itemList.size() - 1) {
                itemObj = new Product( itemList.get(i).getMonth(), itemList.get(i).getYear(), itemCode, "" , quantiti, price,discount,%discount);
                tempListP.add(itemObj);
            }
        
        
    }
    

    return itemList;
}


}
Month | Year | Sku | Detail | Quantity | Price | Discount | % Discount

7 | 2020 | 50607080 | Tshirt | 4 | 40 | 4 | 0.4

8 | 2020 | 10203040 | Tshirt | 6 | 60 | 6 | 0.6

9 | 2020 | 10203040 | Tshirt | 2 | 20 | 2 | 0.2

3 | 2021 | 10203040 | Tshirt | 3 | 30 | 3 | 0.3

8 | 2021 | 10203040 | Tshirt | 8 | 80 | 8 | 0.8
我想创建一个函数,该函数遍历产品列表,并对具有相同月份、年份和sku的项目的值求和,然后返回数量、价格、折扣、%折扣之和的列表

但是,我如何对上述代码进行链计数和求和?特别是对于具有3个以上字段(月、年和sku)的group by。有更好的方法吗

实际上,我需要帮助对值求和,而不仅仅是对列表中的元素进行分组


提前感谢您的帮助。

这个解决方案应该可以工作,我编写了一个比较器来对产品进行排序。然后,如果连续项共享相同的值,则对必要的值求和

比较器

公共类产品Comparator实现Comparator{
公共整数比较(产品p1、产品p2){
int value1=p1.年,与p2.年相比;
如果(值1==0){
int value2=p1.月,与p2.月相比;
如果(值2==0){
返回p1.sku.compareTo(p2.sku);
}否则{
返回值2;
}
}
返回值1;
}
}
求和函数

public static void main(String[] args) {

   List<Products> items = new ArrayList<Products>();

   items.add(new Products(8,2020,10203040,"Tshirt",1,10.00,1.00,0.1));
   items.add(new Products(9,2020,10203040,"Tshirt",2,20.00,2.00,0.2));
   items.add(new Products(3,2021,10203040,"Tshirt",3,30.00,3.00,0.3));
   items.add(new Products(7,2020,50607080,"Tshirt",4,40.00,4.00,0.4));
   items.add(new Products(8,2020,10203040,"Tshirt",5,50.00,5.00,0.5));
   items.add(new Products(8,2021,10203040,"Tshirt",8,80.00,8.00,0.8));

  Products.sumProduct(items);
公共静态列表产品(列表项列表){
List newList=newarraylist();
int指数=-1;
Collections.sort(itemList,newproductscomperator());
对于(产品项:itemList){
if(newList.size()!=0&&newList.get(index.getYear()==item.getYear()&&newList.get(index.getMonth()==item.getMonth()&&newList.get(index.getSku()==item.getSku()){
//增加价值
newList.get(index).setQuanti(newList.get(index).getQuanti()+item.getquantiti());
newList.get(index).setprice(newList.get(index).getprice()+item.getprice());
newList.get(index).setdiscount(newList.get(index).getdiscount()+item.getdiscount());
newList.get(索引).set%折扣(newList.get(索引).get%折扣()+商品.get%折扣());
}否则{
新建列表。添加(项);
索引++;
}
}
返回newList;
}

如果您想在一个返回值中返回这些统计信息,理想情况下,您可以使用包含这些值的结构。这是一个更好的设计,可以重用产品类来包含聚合。这是以一种会让读者感到困惑的方式重载类的使用

这是一个很好的记录用例,因为它们为您做了大量工作(例如,为您的排序和聚合定义
等于
hashCode
)。您可以添加一些方便的函数来简化排序:

record MonthSKU(int month, int year, int sku) {
    MonthSKU(Product product) {
        this(product.month, product.year, product.sku);
    }
}

record ProductStats(double quantity, double price, double discount) {
    static ProductStats ZERO = new ProductStats(0, 0, 0);
    ProductStats(Product product) {
        this(product.quantity, product.price, product.discount);
    }
    ProductStats combine(ProductStats other) {
        return new ProductStats(quantity + other.quantity, price + other.price, discount + other.discount);
    }
}
现在,使用流运算符进行分组和求和非常简单:

Map<MonthSKU,ProductStats> stats = productList.stream()
    .collect(groupingBy(MonthSKU::new, 
        reducing(ProductStats.ZERO, ProductStats::new, ProductStats::combine));
Map stats=productList.stream()
.收集(分组方式)(MonthSKU::新建,
减少(ProductStats.ZERO、ProductStats::new、ProductStats::combine));
在我看来,与包含聚合值的产品列表和需要排序列表的算法相比,对于输出统计而言,这是一个更容易理解的模型-这既难理解又容易打破

该模型也更易于扩展,以添加新字段或更复杂的统计数据(例如,维护计数以计算平均值)。所有这些更改都可以通过更改2条记录来实现,而分组和减少操作完全没有更改

类似地,“详细信息”字段似乎包含与sku相关的信息,而不是与产品相关的信息。如果是这样,您应该将它们放在单独的
映射中或专用的
sku
类中。如果不是这样,则当产品具有相同的sku但不同的详细信息时,您需要确定在汇总摘要中打印什么