直接上代碼
// 在for外查詢 list
List<QueAttr> byIds = queAttrService.findByIds(Lists.newArrayList(attrIds));
Map<Long, String> attrId_name = byIds.stream().filter(Objects::nonNull)
.collect(Collectors.toMap(QueAttr::getAttrId, QueAttr::getName));
for (QueAttrPath queAttrPath : queAttrPaths) {
List<Long> pathIds = queAttrPath.getPathIds();
List<Map<String, String>> names = Lists.newArrayList();
for (Long pathId : pathIds) {
String name = attrId_name.get(pathId);// attrId
// String name = queAttrService.getCacheAttr(pathId).getString("name"); for內查詢 即使查詢redis也比for耗時長
if (StringUtils.isNotBlank(name)) {
Map<String, String> _temp = Maps.newHashMap();
_temp.put("attrId", pathId.toString());
_temp.put("name", name);
names.add(_temp);
}
}
queAttrPath.setName(names);
}