Java中list多对多拆分

两个list多对多拆分

private Map<String,Map<String,Object>> splitAndMatch(List<PaymentOrder> list1,List<RepaymentOfflineOperationCompute> list2){

if (list1 == null){
  list1=new ArrayList<>();
}
if (list2 == null){
  list2=new ArrayList<>();
}
//排序
Collections.sort(list1, new Comparator<PaymentOrder>() {
  @Override
  public int compare(PaymentOrder o1, PaymentOrder o2) {
    return o1.getTransactionDate().compareTo(o2.getTransactionDate());
  }
});
Collections.sort(list2, new Comparator<RepaymentOfflineOperationCompute>() {
  @Override
  public int compare(RepaymentOfflineOperationCompute o1, RepaymentOfflineOperationCompute o2) {
    return o1.getCreateTime().compareTo(o2.getCreateTime());
  }
});

int settlementLength = list1.size();
int paymentFlowLength = list2.size();
Map<String,Map<String,Object>> result = new HashMap<>(paymentFlowLength);

//字段组合
Map<Long,BigDecimal> flowNotMatchAmountMap = list2.stream().collect(Collectors.toMap(RepaymentOfflineOperationCompute::getId,RepaymentOfflineOperationCompute::getAllAmount));

Map<Long,BigDecimal> orderNotMatchAmountMap = list1.stream().collect(Collectors.toMap(PaymentOrder::getId,PaymentOrder::getAllAmount));

//拆分
for (int i = 0; i < paymentFlowLength ; i++) {
  Long internalNo = list2.get(i).getId();
  Map<String,Object> amount = result.get(internalNo);
  if (amount == null) {
    amount = new HashMap<>();
    result.put(internalNo.toString(),amount);
  }
  BigDecimal flowAvailableAmount = flowNotMatchAmountMap.get(internalNo);
  for (int j = 0; j < settlementLength; j++) {
  Long bussNo = list1.get(j).getId();
  BigDecimal orderAvailableAmount = orderNotMatchAmountMap.get(bussNo);

  if (flowAvailableAmount.equals(BigDecimal.ZERO)){
    break;
  }
  if (orderAvailableAmount.equals(BigDecimal.ZERO)){
    continue;
  }
  if (flowAvailableAmount.compareTo(orderAvailableAmount)>=0){
    amount.put(bussNo.toString(),orderAvailableAmount);
    flowAvailableAmount = flowAvailableAmount.subtract(orderAvailableAmount);
    orderAvailableAmount = orderAvailableAmount.subtract(orderAvailableAmount);
  }else{
    amount.put(bussNo.toString(),flowAvailableAmount);
    orderAvailableAmount = orderAvailableAmount.subtract(flowAvailableAmount);
    flowAvailableAmount = flowAvailableAmount.subtract(flowAvailableAmount);
  }
  flowNotMatchAmountMap.put(internalNo,flowAvailableAmount);
  orderNotMatchAmountMap.put(bussNo,orderAvailableAmount);
  }

}
return result;

}




Map<String,Integer> map1 = new HashMap<>();
map1.put("money1",200);
Map<String,Integer> map2 = new HashMap<>();
map2.put("money2",200);

List<Map<String,Integer>> list1 = new ArrayList<>();
list1.add(map1);
list1.add(map2);

  Map<String,Integer> map3 = new HashMap<>();
  map3.put("value1",100);
  Map<String,Integer> map4 = new HashMap<>();
  map4.put("value2",200);
  Map<String,Integer> map5 = new HashMap<>();
  map5.put("value3",100);

  List<Map<String,Integer>> list2 = new ArrayList<>();
  list2.add(map3);
  list2.add(map4);
  list2.add(map5);

  List<UserVo> resultList = new ArrayList<>();
  for(int i=0;i<list2.size();i++){
    String key2 = list2.get(i).keySet().iterator().next();
    //for(String s : list2.get(i).keySet()){
      // key2 = s;
    //}
    Integer value = 0;
    Integer value2 = list2.get(i).get(key2);
    if(value2 <= 0) {
      continue;
    }
    for (int j = 0; j < list1.size(); j++) {
      String key1 = list1.get(j).keySet().iterator().next();
      //for (String s : list1.get(j).keySet()) {
        // key1 = s;
      //}
      Integer value1 = list1.get(j).get(key1);
      if(value1 > 0 && value2 > 0) {
        if (value1 >= value2) {
          value = value2;
          value1 = value1 - value2;
          value2 = 0;
        } else {
          value = value1;
          value2 = value2 - value1;
          value1 = 0;
        }
      list1.get(j).put(key1, value1);
      list2.get(i).put(key2, value2);
      System.out.println("=======" + key2 + "=======" + value);
      UserVo user = new UserVo();
      user.setId(key2);
      user.setName(value.toString());
      user.setAreaName(key1);
      resultList.add(user);
    }
  }
}

System.out.println("拆分结果为:============" + JSON.toJSONString(resultList));

posted @ 2019-10-25 16:56  smj1990  阅读(927)  评论(0编辑  收藏  举报