map 主要是重在生成返回值,forEach 重要每個(gè)對(duì)象的操作。
當(dāng)操作具有副作用時(shí),map 不能使用
// forEach 和 map 的使用場(chǎng)景不同
private static void deleteFile(List<String> files){
files.forEach((String s) -> (new File(s)).delete());
}
map 主要是重在生成返回值,forEach 重要每個(gè)對(duì)象的操作。
當(dāng)操作具有副作用時(shí),map 不能使用
// forEach 和 map 的使用場(chǎng)景不同
private static void deleteFile(List<String> files){
files.forEach((String s) -> (new File(s)).delete());
}