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

时间:2022-02-15 03:29:34

//双重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<String, String> 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循环可以有效提高代码运行效率