2 回答
TA贡献1815条经验 获得超10个赞
老实说,我不确定这的复杂性(我明天会尝试弄清楚):
Set<String> left = nonBeans.stream()
.map(AreaOfInterest::getValue)
.collect(Collectors.toSet());
beans.stream()
.filter(x -> left.contains(x.getValue()))
.forEach(x -> x.setSelected(true));
在这种情况下,复杂性将为O(n).
TA贡献1993条经验 获得超5个赞
假设:
List<AreaOfInterest> list = /* ... */
List<AreaOfInterestBean> listOfBeans = /* ... */
两个简单的迭代可以轻松实现:
for (int i=0; i<listOfBeans.size(); i++) { // Iterate beans
AreaOfInterestBean aoiBean = listOfBeans.get(i); // Get each bean
for (int j=0; j<list.size(); j++) { // Iterate entity
AreaOfInterest aoi = list.get(j); // Get each entity
if (aoiBean.getId() == aoi.getId()) { // If they have the same ID
aoiBean.setValue(aoi.getValue()); // Update the bean value
break; // Go to the next bean
}
}
}
示例:具有以下简化数据:
AreaOfInterest同id和value:1, "Car",2, "Bike",3, "Ship"
AreaOfInterestBeans同id和value:5, "Limo",2, "Scooter",7, "Yacht"
结果AreaOfInterestBeans将是 id 的替换值2:Scooter-> Bike。
AreaOfInterestBeans同id和value:5, "Limo",2, "Bike",7, "Yacht"
另一种流方式:
listOfBeans = listOfBeans.stream()
.peek(aoiBean -> list.stream()
.filter(aoi -> aoi.getId() == aoiBean.getId())
.findFirst()
.ifPresent(i -> aoiBean.setValue(i.getValue())))
.collect(Collectors.toList());
添加回答
举报