java分批处理list

第一种方法:


List<Long> lists= new ArrayList<Long>();//查询出来的list

int cycle = lastIndexRids.size() / 1000;//分批1000
for (int i = 0; i <= cycle; i++) {
int start = i * 1000;
int end = (start + 1000) < lists.size() ? (start + 1000) : lists.size();
params.put("lists", lists.subList(start, end));

if (CollectionUtils.isEmpty(pays)) {
                break;
            }

                        //逻辑处理
List<Map<String, Object>> ridMapBeanList = loanRepaymentMapper.checkDiffCapital(params);

}

第二种方法:

private static final Integer PAGE_SIZE = 1000;//分页处理头寸

int cycle = (payeeList.size()+PAGE_SIZE -1)/PAGE_SIZE;
   for(int i=0;i< cycle;i++){
    int start = i*PAGE_SIZE;
     int end = (start+PAGE_SIZE) < payeeList.size() ? (start+PAGE_SIZE):payeeList.size();
      List<PayeeDto> pays = payeeList.subList(start, end);

if (CollectionUtils.isEmpty(pays)) {
                break;
            }

                        //逻辑处理
List<Map<String, Object>> ridMapBeanList = loanRepaymentMapper.checkDiffCapital(params);

}


第三种方法:

private static int INSERT_SIZE = 1000;

public static List<List<T>> groupListByQuantity(List<T> list) {
        List<List<T>> TListArray = new ArrayList<List<T>>();
        try {
            if (!CollectionUtils.isEmpty(list)) {
                /** 切分每次1000 */
                TListArray = groupListByQuantity(list, INSERT_SIZE);
            } else {
                logger.info("本次批量插入size=0");
            }

        } catch (Exception e) {
            logger.error("插入数据异常.数据", e);
        }
        return TListArray;
    }

public static List groupListByQuantity(List list, int quantity) {
        if (list == null || list.size() == 0) {
            return list;
        }

        if (quantity <= 0) {
            new IllegalArgumentException("error quantity.");
        }

        List wrapList = new ArrayList();
        int count = 0;
        while (count < list.size()) {
            wrapList.add(new ArrayList(list.subList(count, (count + quantity) > list.size() ? list.size() : count + quantity)));
            count += quantity;
        }

        return wrapList;
    }


第四种方法:

int bacthSendNum = 1000;
        if(l.size()<=bacthSendNum){
           //逻辑处理
        } else {
            StringBuilder taskIds = new StringBuilder();
            int z = l.size()%bacthSendNum == 0 ?l.size()/bacthSendNum : l.size()/bacthSendNum+1;
            int start = 0;
            int end = bacthSendNum;
            for (int j = 0; j < z; j++) {
                String taskId = UUID.randomUUID().toString();
                List<String> lTemp = new ArrayList<String>();
                List<String> cTemp = new ArrayList<String>();
                lTemp.addAll(l.subList(start, end));
                cTemp.addAll(c.subList(start, end));
              //逻辑处理
                taskIds.append(taskId).append(",");
                start=end;
                end= end+bacthSendNum > l.size() ? l.size() : end+bacthSendNum;
            }
          
        }



猜你喜欢

转载自blog.csdn.net/damoneric_guo/article/details/71055129