雙層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());
}
}

}get

 

//將集合轉換爲map,key爲sceneCode
Map<String, String> collect = fusRecomConfigDOS.stream().collect(
Collectors.toMap(FusRecomConfigDO::getSceneCode, FusRecomConfigDO::getExistStatuses));it

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

利用stream流來代替雙重for循環能夠有效提升代碼運行效率效率

相關文章
相關標籤/搜索