これは私が複数のフィールドbranchCodeとprdIdでグループ化した方法です。困っている人のために投稿するだけです。
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Product1 {
public BigInteger branchCode;
public BigInteger prdId;
public String accountCode;
public BigDecimal actualBalance;
public BigDecimal sumActBal;
public BigInteger countOfAccts;
public Product1() {
}
public Product1(BigInteger branchCode, BigInteger prdId, String accountCode, BigDecimal actualBalance) {
this.branchCode = branchCode;
this.prdId = prdId;
this.accountCode = accountCode;
this.actualBalance = actualBalance;
}
public BigInteger getCountOfAccts() {
return countOfAccts;
}
public void setCountOfAccts(BigInteger countOfAccts) {
this.countOfAccts = countOfAccts;
}
public BigDecimal getSumActBal() {
return sumActBal;
}
public void setSumActBal(BigDecimal sumActBal) {
this.sumActBal = sumActBal;
}
public BigInteger getBranchCode() {
return branchCode;
}
public void setBranchCode(BigInteger branchCode) {
this.branchCode = branchCode;
}
public BigInteger getPrdId() {
return prdId;
}
public void setPrdId(BigInteger prdId) {
this.prdId = prdId;
}
public String getAccountCode() {
return accountCode;
}
public void setAccountCode(String accountCode) {
this.accountCode = accountCode;
}
public BigDecimal getActualBalance() {
return actualBalance;
}
public void setActualBalance(BigDecimal actualBalance) {
this.actualBalance = actualBalance;
}
@Override
public String toString() {
return "Product{" + "branchCode:" + branchCode + ", prdId:" + prdId + ", accountCode:" + accountCode + ", actualBalance:" + actualBalance + ", sumActBal:" + sumActBal + ", countOfAccts:" + countOfAccts + '}';
}
public static void main(String[] args) {
List<Product1> al = new ArrayList<Product1>();
System.out.println(al);
al.add(new Product1(new BigInteger("01"), new BigInteger("11"), "001", new BigDecimal("10")));
al.add(new Product1(new BigInteger("01"), new BigInteger("11"), "002", new BigDecimal("10")));
al.add(new Product1(new BigInteger("01"), new BigInteger("12"), "003", new BigDecimal("10")));
al.add(new Product1(new BigInteger("01"), new BigInteger("12"), "004", new BigDecimal("10")));
al.add(new Product1(new BigInteger("01"), new BigInteger("12"), "005", new BigDecimal("10")));
al.add(new Product1(new BigInteger("01"), new BigInteger("13"), "006", new BigDecimal("10")));
al.add(new Product1(new BigInteger("02"), new BigInteger("11"), "007", new BigDecimal("10")));
al.add(new Product1(new BigInteger("02"), new BigInteger("11"), "008", new BigDecimal("10")));
al.add(new Product1(new BigInteger("02"), new BigInteger("12"), "009", new BigDecimal("10")));
al.add(new Product1(new BigInteger("02"), new BigInteger("12"), "010", new BigDecimal("10")));
al.add(new Product1(new BigInteger("02"), new BigInteger("12"), "011", new BigDecimal("10")));
al.add(new Product1(new BigInteger("02"), new BigInteger("13"), "012", new BigDecimal("10")));
Map<BigInteger, List<Product1>> groupByBrCd = al.stream().collect(Collectors.groupingBy(Product1::getBranchCode, Collectors.toList()));
System.out.println("\n\n\n" + groupByBrCd);
Map<BigInteger, List<Product1>> groupByPrId = null;
List<Product> finalOutputList = new LinkedList<Product>();
Product newPrd = null;
Iterator<BigInteger> brItr = groupByBrCd.keySet().iterator();
Iterator<BigInteger> prdidItr = null;
BigInteger brCode = null;
BigInteger prdId = null;
Map<BigInteger, List<Product>> tempMap = null;
List<Product1> accListPerBr = null;
List<Product1> accListPerBrPerPrd = null;
Product1 tempPrd = null;
Double sum = null;
while (brItr.hasNext()) {
brCode = brItr.next();
accListPerBr = groupByBrCd.get(brCode);
groupByPrId=accListPerBr.stream().collect(Collectors.groupingBy(Product1::getPrdId, Collectors.toList()));
System.out.println("====================");
System.out.println(groupByPrId);
prdidItr = groupByPrId.keySet().iterator();
while(prdidItr.hasNext()){
prdId=prdidItr.next();
accListPerBrPerPrd=groupByPrId.get(prdId);
newPrd = new Product();
tempPrd = accListPerBrPerPrd.get(0);
newPrd.setBranchCode(tempPrd.getBranchCode());
newPrd.setPrdId(tempPrd.getPrdId());
newPrd.setCountOfAccts(BigInteger.valueOf(accListPerBrPerPrd.size()));
sum = accListPerBrPerPrd.stream().filter(o -> o.getActualBalance() != null).mapToDouble(o -> o.getActualBalance().doubleValue()).sum();
newPrd.setSumActBal(BigDecimal.valueOf(sum));
finalOutputList.add(newPrd);
}
}
System.out.println("+++++++++++++++++++++++");
System.out.println(finalOutputList);
}
}
出力は次のとおりです。
+++++++++++++++++++++++
[Product{branchCode:1, prdId:11, accountCode:null, actualBalance:null, sumActBal:20.0, countOfAccts:2}, Product{branchCode:1, prdId:12, accountCode:null, actualBalance:null, sumActBal:30.0, countOfAccts:3}, Product{branchCode:1, prdId:13, accountCode:null, actualBalance:null, sumActBal:10.0, countOfAccts:1}, Product{branchCode:2, prdId:11, accountCode:null, actualBalance:null, sumActBal:20.0, countOfAccts:2}, Product{branchCode:2, prdId:12, accountCode:null, actualBalance:null, sumActBal:30.0, countOfAccts:3}, Product{branchCode:2, prdId:13, accountCode:null, actualBalance:null, sumActBal:10.0, countOfAccts:1}]
それをフォーマットした後:
[
Product{branchCode:1, prdId:11, accountCode:null, actualBalance:null, sumActBal:20.0, countOfAccts:2},
Product{branchCode:1, prdId:12, accountCode:null, actualBalance:null, sumActBal:30.0, countOfAccts:3},
Product{branchCode:1, prdId:13, accountCode:null, actualBalance:null, sumActBal:10.0, countOfAccts:1},
Product{branchCode:2, prdId:11, accountCode:null, actualBalance:null, sumActBal:20.0, countOfAccts:2},
Product{branchCode:2, prdId:12, accountCode:null, actualBalance:null, sumActBal:30.0, countOfAccts:3},
Product{branchCode:2, prdId:13, accountCode:null, actualBalance:null, sumActBal:10.0, countOfAccts:1}
]