如何將以下代碼轉換爲使用帶流的Java lambda?如何用Java中的lambda和stream替換for循環?
List<Fruit> fruits = createFruitArrayList(); // creates a list of fruits
Fruit largeApple = null; // holds the largest apple so far for
for (Fruit fruit : fruits) {
if (fruit.getType() == 「Apple」) {
if (largeApple == null ||
largeApple.size() < fruit.size()) {
largeApple = fruit;
}
}
}
改進了格式並添加了更多描述性詞語來幫助解決問題。 – AlBlue