java8 partstreamfilterit - !existscontainsitgetCodetoList; 再优化下
为了进一步优化上述代码,可以使用Set数据结构来存储exists列表中的元素,这样可以提高查找的效率。另外,可以使用Collectors.toList()方法来代替.toList(),以更加简洁的方式将过滤后的元素收集到列表中。
下面是优化后的代码:
Set<String> existCodes = exists.stream()
.map(Entity::getCode)
.collect(Collectors.toSet());
List<Entity> filteredList = part.stream()
.filter(it -> !existCodes.contains(it.getCode()))
.collect(Collectors.toList());
这里首先使用exists列表中的元素构建一个Set集合existCodes,其中只包含Entity对象的code属性。然后,使用filter操作将不包含在existCodes集合中的元素过滤出来,并使用Collectors.toList()将过滤后的元素收集到列表中
原文地址: https://www.cveoy.top/t/topic/iBbi 著作权归作者所有。请勿转载和采集!