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();
這是應該工作或你的完整更正代碼!
添加回答
舉報