List<BlogLink> links = blogLinkMapper.findLinkList(null); if (!CollectionUtils.isEmpty(links)) { Map<Byte, List<BlogLink>> linksMap = links.stream().collect(Collectors.groupingBy(BlogLink::getLinkType)); return linksMap; }
时间: 2023-07-15 19:10:02 浏览: 146
这段代码使用了MyBatis框架进行数据库查询,查询结果返回一个BlogLink对象的List。然后通过Java 8的Stream API对这个List进行操作,使用groupingBy方法将其按照BlogLink对象的linkType属性进行分组,最终得到一个Map对象,其中key为linkType属性值,value为相应的BlogLink对象List。如果links为空,则返回一个空Map对象。
相关问题
if (!StringUtil.isEmpty(idCard)) { LambdaQueryWrapper<UserExtend>queryWrapper=new LambdaQueryWrapper<>(); queryWrapper.eq(UserExtend::getIdcard,idCard); List<UserExtend> userList = userExtendMapper.selectList(queryWrapper); if (!CollectionUtils.isEmpty(userList)) { UserExtend userExtend = userList.get(0); String userId = userExtend.getUserId(); userExtend = queryExtendByUserId(userId); List<UserBase> userBases = userBaseMapper.selectUserBaseList(userId); if (!CollectionUtils.isEmpty(userBases)) { userExtend.setUserBase(userBases.get(0)); } return userExtend; } } 如何优化这段代码
根据代码的逻辑,可以将查询语句进行优化,减少查询次数,提高查询效率。可以将以下两个查询合并成一个:
```
LambdaQueryWrapper<UserExtend>queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(UserExtend::getIdcard,idCard);
List<UserExtend> userList = userExtendMapper.selectList(queryWrapper);
```
优化后的代码如下:
```
if (!StringUtil.isEmpty(idCard)) {
LambdaQueryWrapper<UserExtend> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserExtend::getIdcard, idCard);
List<UserExtend> userList = userExtendMapper.selectList(queryWrapper);
if (!CollectionUtils.isEmpty(userList)) {
UserExtend userExtend = userList.get(0);
String userId = userExtend.getUserId();
userExtend = queryExtendByUserId(userId);
List<UserBase> userBases = userBaseMapper.selectUserBaseList(userId);
if (!CollectionUtils.isEmpty(userBases)) {
userExtend.setUserBase(userBases.get(0));
}
return userExtend;
}
}
```
另外,可以考虑使用缓存来提高查询效率,如果用户数据不经常更新,可以将查询结果缓存到内存中,下一次查询时直接从内存中获取。
优化 private void addReferencedKpi(List<SimulationTableDto> list, List<String> codeList, List<Map<String, List<String>>> referencedList) { if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) { return; } // 转换结构 key1-table key2-column value-codeList Map<String, Map<String, List<String>>> tableColumnWithCodeListMap = Maps.newHashMap(); int size = referencedList.size(); for (int i = 0; i < size; i++) { String code = codeList.get(i); Map<String, List<String>> referencedPerMap = referencedList.get(i); if (MapUtils.isEmpty(referencedPerMap)) { continue; } referencedPerMap.forEach((table, columns) -> { tableColumnWithCodeListMap.putIfAbsent(table, Maps.newHashMap()); for (String column : columns) { tableColumnWithCodeListMap.get(table).putIfAbsent(column, Lists.newArrayList()); tableColumnWithCodeListMap.get(table).get(column).add(code); } }); } for (SimulationTableDto simulationTableDto : list) { String simTableName = simulationTableDto.getTableName(); String simTitle = simulationTableDto.getColumn().getTitle(); if (!tableColumnWithCodeListMap.containsKey(simTableName) || !tableColumnWithCodeListMap.get(simTableName).containsKey(simTitle)) { continue; } List<String> referencedCodeList = tableColumnWithCodeListMap.get(simTableName).get(simTitle); simulationTableDto.getColumn().setReferencedKpis(referencedCodeList); } }
可以尝试优化一下代码结构,减少嵌套层数,使得代码更加易读和易维护。例如可以将转换结构的代码抽取成一个单独的方法,用于处理每个 referencedPerMap 中的数据,并返回转换后的表格和列名以及对应的 codeList 的 Map 对象。同时,可以考虑使用 Java 8 的 Stream API,更加简洁地实现循环操作。修改后的代码如下:
```
private void addReferencedKpi(List<SimulationTableDto> list, List<String> codeList, List<Map<String, List<String>>> referencedList) {
if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) {
return;
}
// 转换结构 key1-table key2-column value-codeList
Map<String, Map<String, List<String>>> tableColumnWithCodeListMap = new HashMap<>();
referencedList.forEach(referencedPerMap -> {
if (MapUtils.isEmpty(referencedPerMap)) {
return;
}
referencedPerMap.forEach((table, columns) -> {
tableColumnWithCodeListMap.computeIfAbsent(table, k -> new HashMap<>());
columns.forEach(column -> tableColumnWithCodeListMap.get(table).computeIfAbsent(column, k -> new ArrayList<>()).addAll(codeList));
});
});
list.forEach(simulationTableDto -> {
String simTableName = simulationTableDto.getTableName();
String simTitle = simulationTableDto.getColumn().getTitle();
tableColumnWithCodeListMap.computeIfPresent(simTableName, (table, columnMap) -> {
if (columnMap.containsKey(simTitle)) {
simulationTableDto.getColumn().setReferencedKpis(columnMap.get(simTitle));
}
return columnMap;
});
});
}
```
阅读全文