课程中获取两个id集合的代码是:
List<Long> unitIds = request.getUnitItems().stream()
.map(CreativeUnitRequest.CreativeUnitItem::getUnitId)
.collect(Collectors.toList());
List<Long> creativeIds = request.getUnitItems().stream()
.map(CreativeUnitRequest.CreativeUnitItem::getCreativeId)
.collect(Collectors.toList());
这样的话,request.getUnitItems()这个集合实际上是遍历了两次,对性能上是有损耗的。
感觉用下面这段代码会更好,只遍历一次集合就获取到了两个id的集合:
List<Long> creativeIds = new ArrayList<>();
List<Long> unitIds = new ArrayList<>();
request.getItems().forEach(i -> {
creativeIds.add(i.getCreativeId());
unitIds.add(i.getUnitId());
});