java list 专属组_java – 如何将ArrayList中的元素分组并分成三个List

我有一个实体类

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

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值