如何将ArrayList中的元素分组并分成三个List

Dra*_*gon 5 java grouping element divide

我有一个实体类

    class Entity {
        private String customer;
        private String product;
        private String productDetail;
    }
Run Code Online (Sandbox Code Playgroud)

我有一个ArrayList<Entity>包含许多记录,例如我的列表中的记录:

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
Run Code Online (Sandbox Code Playgroud)

我下面有3个实体

   class ProductDetail{
       private String details;
   }

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

   class Customer{
       private String customer;
       private List<Product> productList;
   }
Run Code Online (Sandbox Code Playgroud)

我想循环我的ArrayList<Entity>和组记录到Customer类,Customer包括productList,productList包括detailList.

请建议我一个解决方案.

更新我的代码:

客户实体:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

产品实体:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

产品细节实体:

public class ProductDetailEntity {
    private int details;

    public int getDetails() {
        return details;
    }

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

}
Run Code Online (Sandbox Code Playgroud)

我的虚拟代码测试:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

mv2*_*580 0

我可以提出下一个决定:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by smv on 19/09/16.
 */
public class MainTest {

    @AllArgsConstructor
    @ToString
    @Data
    class Entity {
        private String customer;
        private String product;
        private String productDetail;
    }

    @AllArgsConstructor
    @ToString
    @Data
    class ProductDetail{
        private String details;
    }

    @AllArgsConstructor
    @ToString
    @Data
    class Product{
        private String product;
        private List<ProductDetail> detailList;
    }

    @AllArgsConstructor
    @ToString
    @Data
    class Customer{
        private String customer;
        private List<Product> productList;
    }
    @Test
    public void run() throws Exception {
        ArrayList<Entity> entities = new ArrayList<>();
        entities.add(new Entity("A", "A1", "A11"));
        entities.add(new Entity("A", "A1", "A12"));
        entities.add(new Entity("A", "A2", "A21"));
        entities.add(new Entity("A", "A2", "A22"));
        entities.add(new Entity("B", "B1", "B11"));
        entities.add(new Entity("B", "B2", "B21"));
        entities.add(new Entity("C", "C1", "C11"));
        entities.add(new Entity("C", "C1", "C12"));

        ArrayList<Customer> customers = new ArrayList<>();
        entities.forEach(entity -> {
            Customer customer = customers.stream().filter(c -> c.getCustomer().equals(entity.getCustomer())).findFirst().orElse(new Customer(entity.getCustomer(), new ArrayList<>()));
            if (!customers.contains(customer)) {
                customers.add(customer);
            }
            Product product = customer.getProductList().stream().filter(p -> p.getProduct().equals(entity.getProduct())).findFirst().orElse(new Product(entity.getProduct(), new ArrayList<>()));
            if (!customer.getProductList().contains(product)) {
                customer.getProductList().add(product);
            }
            ProductDetail productDetail = product.getDetailList().stream().filter(pd -> pd.getDetails().equals(entity.getProductDetail())).findFirst().orElse(new ProductDetail(entity.getProductDetail()));
            if (!product.getDetailList().contains(productDetail)) {
                product.getDetailList().add(productDetail);
            }
        });

        customers.forEach(s -> System.out.println(s));
    }

}
Run Code Online (Sandbox Code Playgroud)

我认为这不是最好的变体,但它确实有效。我认为必须有更优雅的方式来替换if代码。

UPD

数据bean 上的注释Lombok lib 注释提供了声明bean 的便捷方法。它也可以在 java 7 中运行。

@AllArgsConstructor - adds the constructor with all fields.
@Data - generate getters/setters for all fields.
@ToString - add the method that returns the string representation of the bean.
Run Code Online (Sandbox Code Playgroud)

更多语法仅适用于 java 8。这是List.forEach带有 lambda 表达式的函数,显然,它会迭代列表,并且可以用for循环代替。每个数据 bean 都是使用 Java 8 流 API(filter带有谓词表达式)和可选类型 ( orElse) 来查找或创建的。所以这一行:

Customer customer = customers.stream().filter(c -> c.getCustomer().equals(entity.getCustomer())).findFirst().orElse(new Customer(entity.getCustomer(), new ArrayList<>()));
Run Code Online (Sandbox Code Playgroud)

在 java 7 中可以替换为:

Customer customer = null;
for (Customer c: customers ) {
        if(c.getCustomer().equals(entity.getCustomer())) {
            customer = c;
            break;
        }
    }
if (customer == null) {
  customer  = new Customer(entity.getCustomer(), new ArrayList<>())
}
Run Code Online (Sandbox Code Playgroud)

等等..