java list拆分_Java中list多对多拆分

两个list多对多拆分

private Map> splitAndMatch(List list1,List list2){

if (list1 == null){

list1=new ArrayList<>();

}

if (list2 == null){

list2=new ArrayList<>();

}

//排序

Collections.sort(list1, new Comparator() {

@Override

public int compare(PaymentOrder o1, PaymentOrder o2) {

return o1.getTransactionDate().compareTo(o2.getTransactionDate());

}

});

Collections.sort(list2, new Comparator() {

@Override

public int compare(RepaymentOfflineOperationCompute o1, RepaymentOfflineOperationCompute o2) {

return o1.getCreateTime().compareTo(o2.getCreateTime());

}

});

int settlementLength = list1.size();

int paymentFlowLength = list2.size();

Map> result = new HashMap<>(paymentFlowLength);

//字段组合

Map flowNotMatchAmountMap = list2.stream().collect(Collectors.toMap(RepaymentOfflineOperationCompute::getId,RepaymentOfflineOperationCompute::getAllAmount));

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

//拆分

for (int i = 0; i < paymentFlowLength ; i++) {

Long internalNo = list2.get(i).getId();

Map 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 map1 = new HashMap<>();

map1.put("money1",200);

Map map2 = new HashMap<>();

map2.put("money2",200);

List> list1 = new ArrayList<>();

list1.add(map1);

list1.add(map2);

Map map3 = new HashMap<>();

map3.put("value1",100);

Map map4 = new HashMap<>();

map4.put("value2",200);

Map map5 = new HashMap<>();

map5.put("value3",100);

List> list2 = new ArrayList<>();

list2.add(map3);

list2.add(map4);

list2.add(map5);

List resultList = new ArrayList<>();

for(int i=0;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));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值