Repository navigation
Expand file tree
/
Copy pathNumericStreams.java
More file actions
71 lines (55 loc) · 2.04 KB
/
Copy pathNumericStreams.java
File metadata and controls
71 lines (55 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.brianway.learning.java8.streamapi.usage;
import com.brianway.learning.java8.streamapi.Dish;
import static com.brianway.learning.java8.streamapi.Dish.menu;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* 数值流
* 原始类型特化
* 数值范围
*/
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 = menu.stream()
.mapToInt(Dish::getCalories)
.sum();
System.out.println("Number of calories:" + calories);
// max and OptionalInt
OptionalInt maxCalories = 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);
// 直接使用 orElse
System.out.print("使用 orElse: ");
System.out.println(maxCalories.orElse(1));
// 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 -> isPerfectSquare(a * a + b * b))
.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;
}
}