双层for循环用java中的stream流来实现

//双重for循环
for (int i = 0; i < fusRecomConfigDOList.size(); i++) {
for (int j = 0; j < fusRecomConfigDOS.size(); j++) {
if(fusRecomConfigDOList.get(i).getSceneCode().equals(fusRecomConfigDOS.get(j).getSceneCode())){
fusRecomConfigDOList.get(i).setExistStatuses(fusRecomConfigDOS.get(j).getExistStatuses());
}
}

}

 

//将集合转换为map,key为sceneCode
Map collect = fusRecomConfigDOS.stream().collect(
Collectors.toMap(FusRecomConfigDO::getSceneCode, FusRecomConfigDO::getExistStatuses));

//遍历原先集合,改变exitStatus的值
fusRecomConfigDOList.forEach(
fusRecomConfigDO -> {
if (collect.containsKey(fusRecomConfigDO.getSceneCode())) {
fusRecomConfigDO.setExistStatuses(collect.get(fusRecomConfigDO.getSceneCode()));
}
}
);

利用stream流来代替双重for循环可以有效提高代码运行效率

转载于:https://www.cnblogs.com/panbingqi/p/10935963.html

你可能感兴趣的:(双层for循环用java中的stream流来实现)