Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group element in ArrayList and divide into three List

I have an entity class

    class Entity {
        private String customer;
        private String product;
        private String productDetail;
    }

I have an ArrayList<Entity> including many records, for example record in my list:

customer    product    productDetail
   A          A1            A11
   A          A1            A12
   A          A2            A21
   A          A2            A22
   B          B1            B11
   B          B2            B21
   C          C1            C11
   C          C1            C12

I have 3 entities below

   class ProductDetail{
       private String details;
   }

   class Product{
       private String product;
       private List<ProductDetail> detailList;
   }

   class Customer{
       private String customer;
       private List<Product> productList;
   }

I want to loop through my ArrayList<Entity> and group records into Customer class, Customer class includes productList, and productList includes detailList.

Please suggest me a solution.

Update my code:

Customer entity:

public class CustomerEntity {
    private int customer;

    private List<ProductEntity> productList;

    public int getCustomer() {
        return customer;
    }

    public void setCustomer(int customer) {
        this.customer = customer;
    }

    public List<ProductEntity> getProductList() {
        return productList;
    }

    public void setProductList(List<ProductEntity> productList) {
        this.productList = productList;
    }

}

Product Entity:

public class ProductEntity {

    private int product;
    private List<ProductDetailEntity> detailList;

    public int getProduct() {
        return product;
    }

    public void setProduct(int product) {
        this.product = product;
    }

    public List<ProductDetailEntity> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<ProductDetailEntity> detailList) {
        this.detailList = detailList;
    }

}

Product detail entity:

public class ProductDetailEntity {
    private int details;

    public int getDetails() {
        return details;
    }

    public void setDetails(int details) {
        this.details = details;
    }

}

My dummy code test:

public class TestList {
    public static void main(String[] args) {
        TestEntity testEntity;
        List<TestEntity> list = new ArrayList<TestEntity>();

        // Create Dummy Data
        testEntity = new TestEntity();
        testEntity.setCustomer("A");
        testEntity.setProduct("A1");
        testEntity.setProductDetail("A11");
        list.add(testEntity);

        testEntity = new TestEntity();
        testEntity.setCustomer("A");
        testEntity.setProduct("A1");
        testEntity.setProductDetail("A12");
        list.add(testEntity);

        testEntity = new TestEntity();
        testEntity.setCustomer("A");
        testEntity.setProduct("A2");
        testEntity.setProductDetail("A21");
        list.add(testEntity);

        testEntity = new TestEntity();
        testEntity.setCustomer("A");
        testEntity.setProduct("A2");
        testEntity.setProductDetail("A22");
        list.add(testEntity);

        testEntity = new TestEntity();
        testEntity.setCustomer("B");
        testEntity.setProduct("B1");
        testEntity.setProductDetail("B11");
        list.add(testEntity);

        testEntity = new TestEntity();
        testEntity.setCustomer("B");
        testEntity.setProduct("B2");
        testEntity.setProductDetail("B21");
        list.add(testEntity);

        testEntity = new TestEntity();
        testEntity.setCustomer("C");
        testEntity.setProduct("C1");
        testEntity.setProductDetail("C11");
        list.add(testEntity);

        testEntity = new TestEntity();
        testEntity.setCustomer("C");
        testEntity.setProduct("C1");
        testEntity.setProductDetail("C12");
        list.add(testEntity);

        Map<String, List<TestEntity>> groupByCustomerMap = new LinkedHashMap<String, List<TestEntity>>();
        List<TestEntity> tempEntityList;
        TestEntity tempEntity;

        // Group record by customer
        for (TestEntity item : list) {
            String customer = item.getCustomer();

            tempEntityList = new ArrayList<TestEntity>();
            tempEntity = new TestEntity();

            tempEntity.setCustomer(customer);
            tempEntity.setProductDetail(item.getProductDetail());
            tempEntity.setProduct(item.getProduct());

            tempEntityList.add(tempEntity);

            if (!groupByCustomerMap.containsKey(customer)) {
                groupByCustomerMap.put(customer, tempEntityList);
            } else {
                groupByCustomerMap.get(customer).addAll(tempEntityList);
            }
        }

        // I think from groupByCustomerMap, read ProductDetail and group by product
        // Pleaes suggest me next solution
    }
}
like image 679
Dragon Avatar asked Nov 09 '22 08:11

Dragon


1 Answers

A typical approach that I would try is the following:

  • Create an empty list of customers
  • Create an empty list of products
  • Iterate over input array list consisting of Entity elements.
    • If the customer contained in the current entity is not already in the list of customers, then create and add the customer. Then check for the product. If the customer was created, then the productList should be empty and the product needs to be added. If the customer was already existing, check for the existence of the product in the list of products....
    • For filling the list of products you can choose a similar approach. The basic checks are the same.
like image 105
Stefan Freitag Avatar answered Nov 14 '22 21:11

Stefan Freitag