如何将这些值分组或合并为 java 中的单个记录?

How to group or merge these values into single record in java?

我从数据库中检索记录并将其存储在 Iterable 中。它是 'Product' 类型。即,

Iterable<Product> iterable;

 Name        Price   Description    Category
 Samsung     20,000  Its tv          TV
 Sony        15,000  Smart 3d tv     TV
 Moto        19,000   Its android    Mobile
 LG          18,000   Its pc         PC

它包含多行。我需要按 'category' 列对这些记录进行分组。如果类别在多个记录中具有相同的值,则需要合并该记录。该行的 'Price' 列的值应该相加,并且 'Description' 也应该连接起来。我需要在 java.How 中做什么才能做到 java? 我想要的输出是....

  Name            Price      Description                Category
  Samsung, Sony   35,000     Its tv, Smart 3d tv          TV
  Moto            19,000     Its android                Mobile
  LG              18,000     Its pc                       PC

你将不得不做这样的事情..

希望您的 Product class 将具有以下属性的 getter 和 setter..

  • 姓名
  • 价格
  • desc

    List<String> products = getProducts();/*your List of products*/
    
    HashMap<String, Product> mergedProducts = new HashMap<String, Product>();
    for(Product product : products){
        if(mergedProducts.containsKey(product.getCategory())){
            Product existingProduct = mergedProducts.get(product.getCategory());
    
            String newName = existingProduct.getName() + ", " + product.getName();
            int newPrice = existingProduct.getPrice() + product.getPrice();
            String newDesc = existingProduct.getDesc() + ", " + product.getDesc();
    
            existingProduct.setName(newName);
            existingProduct.setPrice(newPrice);
            existingProduct.setDesc(newDesc);
        } else {
            mergedProducts.put(product.getCategory(), product);
        }
    }
    

假设您的 class 是

class Product{

    private String name;
    private String decription;
    private int price;
    private String category;

 // getters and setters
}

创建另一个 class ProductDetail

class ProductDetail{

    private String name;
    private String decription;
    private int totalPrice;
    //  getters and setters
}

Java逻辑

    List<Product> productList = getProductList();
    Map<String, ProductDetail> map = new HashMap<String, ProductDetail>();

    for(Product product : productList){

        ProductDetail productDetail = map.get(product.getCategory());

        if(productDetail == null ){
            productDetail = new ProductDetail();
            productDetail.setName(product.getName());
            productDetail.setDecription(product.getDecription());
            productDetail.setTotalPrice(product.getPrice());
            map.put(product.getCategory(), productDetail);
        }else{
            String name = product.getName() + " , " + productDetail.getName();
            String description = product.getDecription() + " , " + productDetail.getDecription();
            int totalPrice = product.getPrice() + productDetail.getTotalPrice();
            productDetail.setDecription(description);
            productDetail.setName(name);
            productDetail.setTotalPrice(totalPrice);
        }
    }