java如何将ArrayList中的元素分组并分成三个列表
我有一门实体课
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<ProductDetail> detailList;
}
class Customer{
private String customer;
private List<Product> productList;
}
我想把我的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;
}
}
产品实体:
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;
}
}
产品详细信息实体:
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<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
}
}
共 (0) 个答案