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
}
}
A typical approach that I would try is the following:
Entity
elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With