双重for循环换成lambda写法如何?

本文介绍了一种将双重for循环转换为更高效的Lambda表达式的方法。通过使用Java 8的Stream API,实现了对产品详细信息批量处理的功能,提高了代码的可读性和执行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	/**
	 * 双重for循环写法,嵌套
	 */
	public String save(@Validated ProductX ProductX, HttpServletRequest request) {
		List<ProductXRecord> ppX = ProductX.getProductXRecordList();
		List<String> strsToListX= Arrays.asList(request.getParameterValues("detailValueXX"));
		List<List<String>> columnList = GetListUtils.getList(strsToListX,strsToListX.size()/ppX.size());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");
		Map<String, Object> delX = new HashedMap();
		delX.put("product_x_id",ProductX.getId());
		ProductXService.delXColumnValue(delX);
		for (int i = 0; i < ppX.size(); i++) {
			String recordId = ppX.get(i).getId();
			for (int j = 0; j < columnList.get(i).size(); j++) {
				String detail_value_x = (StringUtils.isBlank(columnList.get(i).get(j)) ? "0" :columnList.get(i).get(j));
				Map<String, Object> map = new HashedMap();
				map.put("id",UUID.randomUUID().toString());
				map.put("product_x_id",ProductX.getId());
				map.put("product_x_x_id",recordId);
				map.put("detail_value_x",detail_value_x);
				map.put("create_date",sdf.format(new Date()));
				ProductXService.insertColumnValue(map);
			}
		}
		ProductXService.save(ProductX);
		return renderResult(Global.TRUE, text("成功!"));
	}

	/**
	 * 双重for循环换成lambda表达式的写法:
	 */
		List<ProductXRecord> ppX = ProductX.getProductXRecordList();
		List<String> strsToListX= ProductX.getstrsToListX();
		List<List<String>> columnList = GetListUtils.getList(strsToListX,strsToListX.size()/ppX.size());
		Map<String, Object> delX = new HashedMap();
		delX.put("product_x_id",ProductX.getId());
		ProductXRecordDao.delXColumnValue(delX);
		List<ProductXRecordDetail> batchList = new ArrayList<>();
		ProductXRecordDetail pprd = new ProductXRecordDetail();
		ppX.stream().forEach(action -> {
			columnList.get(ppX.indexOf(action)).stream().forEach(action1 -> {
				ProductX pp = new ProductX();
				pp.setId(ProductX.getId());
				pprd.setProductX(pp);
				ProductXRecord ppd = new ProductXRecord();
				ppd.setId(action.getId());
				pprd.setProductXRecord(ppd);
				pprd.setDetailValue(Double.valueOf(StringUtils.isBlank(action1) ? "0" :action1));
				pprd.setId(UUID.randomUUID().toString());
				batchList.add(pprd);
			});
		});
		ProductXRecordDetailDao.insertBatch(batchList);

 

# 优先级名称映射 priority_name_map = { 'other_priority': '其它优先级', 'NONE': '无优先级', 'summary': '总计' } # 初始化结果列表 b = [] # 确定测试类型的处理顺序 test_types_order = sorted( {test_type for project in a.values() for test_type in project}, key=lambda x: (not x.startswith('security_'), x) ) # 动态获取项目顺序 projects_order = list(a.keys()) # 用于跟踪已处理的测试类型 handled_test_types = set() for test_type in test_types_order: # 每个测试类型维护独立的已处理项目集合 handled_projects = set() # 标记当前测试类型名称是否已显示 test_type_printed = False for project in projects_order: if project not in a or test_type not in a[project]: continue test_data = a[project][test_type] # 检查项目是否在当前测试类型中已处理过 project_handled = project in handled_projects # 收集优先级项:P1, P2, P3, 其他 priorities = [p for p in ['P1', 'P2', 'P3'] if p in test_data] others = [k for k in test_data if k not in priorities + ['summary']] group_rows = [] # 标记当前项目的名称是否已显示 project_printed = False for key in priorities + others: data = test_data[key] priority_name = priority_name_map.get(key, key) # 处理测试类型列:仅在当前测试类型的首行显示 current_test_type = test_type if (not test_type_printed and not group_rows) else '' if current_test_type: test_type_printed = True # 标记测试类型已显示 # 处理项目列:仅在当前项目的首行显示 current_project = project if (not project_handled and not project_printed and not group_rows) else '' if current_project: project_printed = True # 标记项目名称已显示 # 生成行数据 row = [ '*', current_test_type, current_project, priority_name, data['total_api'], data['run_api'], data['total_case'], data['run_case'] ] group_rows.append(row) # 处理总计行:测试类型和项目名称均置空 summary = test_data['summary'] summary_row = [ '*', '', '', '总计', summary['total_api'], summary['run_api'], summary['total_case'], summary['run_case'] ] group_rows.append(summary_row) b.extend(group_rows) # 标记当前项目为已处理(针对当前测试类型) handled_projects.add(project) # 标记当前测试类型为已处理 handled_test_types.add(test_type) test_types_order的值只有security_over_permit、security_sql_inject、function这三种,按照这个顺序读取, # 确定测试类型的处理顺序 test_types_order = sorted( {test_type for project in a.values() for test_type in project}, key=lambda x: (not x.startswith('security_'), x) )该部分代码换个写法
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值