List.subList用法,以及多个LIST<T>相加问题解决方案

import java.util.ArrayList;
import java.util.List;

import test.FlowTaskStatusEnum;


public class BMW {
	
	//宝马车当然也可以开动了
	public void run(){
	System.out.println("宝马汽车开始运行...");
	}
	
	
	public static void main(String[] args) {
		List<Integer> shopIdList =new ArrayList<Integer>();
		shopIdList.add(2);
		shopIdList.add(12);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(1);
		
		shopIdList.add(6);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(6);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(7);
		shopIdList.add(9);
		
		
		shopIdList.add(6);
		
		shopIdList.add(2);
		shopIdList.add(6);
		
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(2);
		shopIdList.add(12);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(1);
		
		shopIdList.add(6);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(6);
		shopIdList.add(1);
		shopIdList.add(1);
		shopIdList.add(7);
		shopIdList.add(9);
		
		
		shopIdList.add(6);
		
		shopIdList.add(2);
		shopIdList.add(6);
		
		shopIdList.add(1);
		shopIdList.add(1);
		//System.out.println(shopIdList.size());
	    //double s=shopIdList.size()/10;
	   // System.out.println(s);
	    
	    
	    int i = 0;
	    List<Integer> lisint=new ArrayList<Integer>();
	    
	    Benz benz=new Benz();
        // WARNING 参数过长搜索不接受
        do {
        	Benz bz=new Benz();
            List<Integer> subList = (null == shopIdList) ? new ArrayList<Integer>() :
            	shopIdList.subList(i, Math.min(shopIdList.size(), i + 10));
            System.out.println("i:"+i+"min"+Math.min(shopIdList.size(), i + 10));
            lisint.addAll(subList);
            	 i+=10;
        } while (null != shopIdList && i < shopIdList.size());
        benz.setListInt(lisint);
		System.out.println(benz.getListInt().size());
	}


}


import java.util.List;


public class Benz {//奔驰
	
	//汽车肯定会跑
	public void run(){
	System.out.println("奔驰汽车开始运行...");
	}
	
	private List<String> listStr;
	private List<Integer> listInt;
	public List<String> getListStr() {
		return listStr;
	}
	public void setListStr(List<String> listStr) {
		this.listStr = listStr;
	}
	public List<Integer> getListInt() {
		return listInt;
	}
	public void setListInt(List<Integer> listInt) {
		this.listInt = listInt;
	}
	
	
	


}


private void generateDpAsset(List<DebtBusinessDayReportWholeAssetVO> wholeVO, String dateDt) { // 1. 参数校验 if (CollectionUtils.isEmpty(wholeVO)) { logger.warn("输入的 wholeVO 数据为空,跳过 generateDpAsset 处理"); return; } if (StringUtils.isEmpty(dateDt)) { logger.error("日期 dateDt 不能为空"); throw new IllegalArgumentException("dateDt is required"); } // 按部门分组数据 Map<String, List<DebtBusinessDayReportWholeAssetVO>> deptDataMap = new HashMap<>(); for (DebtBusinessDayReportWholeAssetVO vo : wholeVO) { if (vo == null || vo.getDept() == null) continue; String dept = vo.getDept().trim(); deptDataMap.computeIfAbsent(dept, k -> new ArrayList<>()).add(vo); } List<LiquidReportChart> chartList = new ArrayList<>(); int globalRowNum = 1; // 按固定顺序处理每个部门 for (String dept : departments) { List<DebtBusinessDayReportWholeAssetVO> deptData = deptDataMap.getOrDefault(dept, Collections.emptyList()); // 如果部门没有数据,跳过处理 if (CollectionUtils.isEmpty(deptData)) { logger.info("部门 {} 没有数据,跳过处理", dept); continue; } List<LiquidReportChart> deptCharts = new ArrayList<>(); int rowNum = 1; // --- 开始处理该部门的数据 --- // 按业务大类分组 Map<String, List<DebtBusinessDayReportWholeAssetVO>> bigcMap = new HashMap<>(); List<DebtBusinessDayReportWholeAssetVO> others = new ArrayList<>(); for (DebtBusinessDayReportWholeAssetVO vo : deptData) { String bigc = Optional.ofNullable(vo.getBusiBigcName()).orElse("").trim(); String smlc = Optional.ofNullable(vo.getBusiSmlcName()).orElse("").trim(); // 过滤"其他"或空业务小类 if ("其他".equals(bigc) || "其他".equals(smlc) || StringUtils.isEmpty(smlc)) { others.add(vo); continue; } bigcMap.computeIfAbsent(bigc, k -> new ArrayList<>()).add(vo); } // 获取业务大类数量 int bigcCount = bigcMap.size(); if (bigcCount == 0) { logger.warn("部门 {} 没有有效的业务大类数据", dept); continue; } // 每个业务大类分配的行数(总共22行) int rowsPerBigc = 22 / bigcCount; // 每个业务大类可展示的业务小类数量(分配的行数-2,留2行给合计和其他) int maxSmallTypesPerBigc = Math.max(1, rowsPerBigc - 2); logger.info("部门 {} 共有 {} 个业务大类,每个大类分配 {} 行,最多展示 {} 个业务小类", dept, bigcCount, rowsPerBigc, maxSmallTypesPerBigc); // 处理每个业务大类 for (Map.Entry<String, List<DebtBusinessDayReportWholeAssetVO>> bigcEntry : bigcMap.entrySet()) { String bigcName = bigcEntry.getKey(); List<DebtBusinessDayReportWholeAssetVO> bigcData = bigcEntry.getValue(); // 按业务小类合并数据 Map<String, DebtBusinessDayReportWholeAssetVO> smlcMap = new LinkedHashMap<>(); for (DebtBusinessDayReportWholeAssetVO vo : bigcData) { String smlcName = Optional.ofNullable(vo.getBusiSmlcName()).orElse("").trim(); if (StringUtils.isEmpty(smlcName)) continue; String key = smlcName; DebtBusinessDayReportWholeAssetVO existing = smlcMap.get(key); if (existing != null) { mergeVo(existing, vo); } else { smlcMap.put(key, cloneVo(vo)); } } List<DebtBusinessDayReportWholeAssetVO> smlcList = new ArrayList<>(smlcMap.values()); int smlcCount = smlcList.size(); // 按金额排序 smlcList.sort((a, b) -> b.getAmt().compareTo(a.getAmt())); // 添加业务小类明细 List<DebtBusinessDayReportWholeAssetVO> displayList = new ArrayList<>(); List<DebtBusinessDayReportWholeAssetVO> hiddenList = new ArrayList<>(); if (smlcCount <= maxSmallTypesPerBigc) { // 业务小类数量小于等于最大展示数量,全部展示 displayList.addAll(smlcList); } else { // 业务小类数量超过最大展示数量,只展示前(maxSmallTypesPerBigc-2)个 int displayCount = Math.max(1, maxSmallTypesPerBigc - 2); displayList.addAll(smlcList.subList(0, displayCount)); hiddenList.addAll(smlcList.subList(displayCount, smlcCount)); } // 添加展示的业务小类 for (DebtBusinessDayReportWholeAssetVO vo : displayList) { LiquidReportChart chart = createChartFromVO(vo, dateDt, "chart0040", rowNum++); chart.setChartName(dept); deptCharts.add(chart); } // 添加"其他"行(如果有隐藏的业务小类) if (!hiddenList.isEmpty()) { DebtBusinessDayReportWholeAssetVO otherVo = new DebtBusinessDayReportWholeAssetVO(); otherVo.setDept(dept); otherVo.setBusiBigcName(bigcName); otherVo.setBusiSmlcName("其他"); otherVo.setProjType("其他"); for (DebtBusinessDayReportWholeAssetVO vo : hiddenList) { mergeVo(otherVo, vo); } LiquidReportChart otherChart = createChartFromVO(otherVo, dateDt, "chart0040", rowNum++); otherChart.setChartName(dept); deptCharts.add(otherChart); } // 添加合计行 DebtBusinessDayReportWholeAssetVO totalVo = new DebtBusinessDayReportWholeAssetVO(); totalVo.setDept(dept); totalVo.setBusiBigcName(bigcName); totalVo.setBusiSmlcName("合计"); totalVo.setProjType("合计"); // 合并所有业务小类数据 for (DebtBusinessDayReportWholeAssetVO vo : smlcList) { mergeVo(totalVo, vo); } LiquidReportChart totalChart = createChartFromVO(totalVo, dateDt, "chart0040", rowNum++); totalChart.setCellValue2("合计"); totalChart.setCellValue6("合计"); totalChart.setChartName(dept); deptCharts.add(totalChart); } // 添加部门级别的"其他"数据 if (!others.isEmpty()) { DebtBusinessDayReportWholeAssetVO otherVo = new DebtBusinessDayReportWholeAssetVO(); otherVo.setDept(dept); otherVo.setBusiBigcName("其他"); otherVo.setBusiSmlcName("——"); otherVo.setProjType("——"); for (DebtBusinessDayReportWholeAssetVO vo : others) { mergeVo(otherVo, vo); } LiquidReportChart otherChart = createChartFromVO(otherVo, dateDt, "chart0040", rowNum++); otherChart.setChartName(dept); deptCharts.add(otherChart); } updateChart0040CellValue3To5WithGroupSumForDept(deptCharts, dateDt); // 设置全局行号并加入主列表 for (LiquidReportChart c : deptCharts) { c.setRowNum(globalRowNum++); } chartList.addAll(deptCharts); } // 落库 try { if (!chartList.isEmpty()) { liquidReportChartRepository.save(chartList); logger.info("成功将 {} 条部门明细数据 ({}, date={}) 落库", chartList.size(), "chart0040", dateDt); } else { logger.warn("没有有效数据生成部门明细表 (date={})", dateDt); } } catch (Exception e) { logger.error("保存部门明细数据时发生异常 (date={})", dateDt, e); throw new RuntimeException("Failed to save department asset detail data", e); } } 这段代码的功能
最新发布
11-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值