原来的代码
List<CategoryAttr> categoryAttrs ;
List<GroupAttr> groupAttrs ;
List<GroupAttr> groupAttrsNew = new ArrayList<>();
for (GroupAttr groupAttr : groupAttrs) {
for (CategoryAttr categoryAttr : categoryAttrs) {
if (categoryAttr.getAttrId().equals(groupAttr.getAttrId())) {
groupAttrsNew.add(groupAttr);
}
}
}
groupAttrs.removeAll(groupAttrsNew);
}
代码展现方式不够整洁
优化后
List<CategoryAttr> categoryAttrs;
List<GroupAttr> groupAttrs ;
Set<String> categoryAttrIds = categoryAttrs.stream().map(a -> a.getAttrId()).collect(Collectors.toSet());
List<GroupAttr> attrBos = groupAttrs.stream().filter(a -> !categoryAttrIds.contains(a.getAttrId())).collect(Collectors.toList());```