1 回答
TA贡献1770条经验 获得超3个赞
这样的事情适合您的需要吗?
List<Labels> dbRecord = roleRecord.getLabels();
List<Labels> userRecord = role.getLabels();
Map<String, Labels> labelsByName = dbRecord.stream()
.collect(Collectors.toMap(this::getCorrectedLabelName, i -> i));
userRecord.stream()
.filter(label -> labelsByName.containsKey(getCorrectedLabelName(label)))
.forEach(label -> labelsByName.put(getCorrectedLabelName(label), label));
Collection<Labels> updatedLabels = labelsByName.values();
和
private String getCorrectedLabelName(Labels label) {
return label.getLabelName().replaceAll("\\s+", "").toLowerCase();
}
首先,您使用列表按名称创建标签映射dbRecord。userRecord然后您可以轻松地从中找到与名称匹配的那些,并为它们中的每一个替换地图中的标签。
最后你收集地图值!
PS:这都是假设Labels没有重名!
希望这可以帮助
- - 编辑
List<Labels> dbRecord = roleRecord.getLabels();
List<Labels> userRecord = role.getLabels();
// create Map from dbRecord
Map<String, Labels> labelsByName = dbRecord.stream()
.collect(Collectors.toMap(this::getCorrectedLabelName, i -> i));
userRecord
// for each of userRecord
.forEach(userRecordLabel -> {
// if in dbRecord, update dbRecord labels
if(labelsByName.containsKey(getCorrectedLabelName(userRecordLabel))){
Labels dbRecordLabel = labelsByName.get(getCorrectedLabelName(userRecordLabel));
dbRecordLabel.setAccess(userRecordLabel.getAccess());
dbRecordLabel.setMatch(userRecordLabel.getMatch());
labelsByName.put(getCorrectedLabelName(userRecordLabel), dbRecordLabel);
}else{// if not then add to map
labelsByName.put(getCorrectedLabelName(userRecordLabel), userRecordLabel);
}
});
// final complete list
Collection<Labels> updatedLabels = labelsByName.values();
这是应该工作或你的完整更正代码!
添加回答
举报