我有一个实体类
class Entity {
private String customer;
private String product;
private String productDetail;
}
我有一个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
我下面有3个实体
class ProductDetail{
private String details;
}
class Product{
private String product;
private List detailList;
}
class Customer{
private String customer;
private List productList;
}
我想遍历我的ArrayList< Entity>并将记录分组到Customer类中,Customer类包括productList,productList包括detailList.
请建议我一个解决方案.
更新我的代码:
客户实体:
public class CustomerEntity {
private int customer;
private List productList;
public int getCustomer() {
return customer;
}
public void setCustomer(int customer) {
this.customer = customer;
}
public List getProductList() {
return productList;
}
public void setProductList(List productList) {
this.productList = productList;
}
}
产品实体:
public class ProductEntity {
private int product;
private List detailList;
public int getProduct() {
return product;
}
public void setProduct(int product) {
this.product = product;
}
public List getDetailList() {
return detailList;
}
public void setDetailList(List detailList) {
this.detailList = detailList;
}
}
产品细节实体:
public class ProductDetailEntity {
private int details;
public int getDetails() {
return details;
}
public void setDetails(int details) {
this.details = details;
}
}
我的虚拟代码测试:
public class TestList {
public static void main(String[] args) {
TestEntity testEntity;
List list = new ArrayList();
// 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> groupByCustomerMap = new LinkedHashMap>();
List tempEntityList;
TestEntity tempEntity;
// Group record by customer
for (TestEntity item : list) {
String customer = item.getCustomer();
tempEntityList = new ArrayList();
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
}
}