代码拉取完成,页面将自动刷新
package com.lun.c05;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import com.lun.c04.Dish;
public class NumericStreams {
public static void main(String... args) {
List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);
Arrays.stream(numbers.toArray()).forEach(System.out::println);
int calories = Dish.menu.stream().mapToInt(Dish::getCalories).sum();
System.out.println("Number of calories:" + calories);
// max and OptionalInt
OptionalInt maxCalories = Dish.menu.stream().mapToInt(Dish::getCalories).max();
int max;
if (maxCalories.isPresent()) {
max = maxCalories.getAsInt();
} else {
// we can choose a default value
max = 1;
}
System.out.println(max);
// numeric ranges
IntStream evenNumbers = IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0);
System.out.println(evenNumbers.count());
Stream<int[]> pythagoreanTriples = IntStream.rangeClosed(1, 100).boxed()
.flatMap(a -> IntStream.rangeClosed(a, 100)
.filter(b -> Math.sqrt(a * a + b * b) % 1 == 0).boxed()
.map(b -> new int[] { a, b, (int) Math.sqrt(a * a + b * b) }));
pythagoreanTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));
}
public static boolean isPerfectSquare(int n) {
return Math.sqrt(n) % 1 == 0;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。