forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntStreamDemo.java
More file actions
32 lines (23 loc) · 1.08 KB
/
IntStreamDemo.java
File metadata and controls
32 lines (23 loc) · 1.08 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
package stream;
import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class IntStreamDemo {
public static void main(String[] args) {
int[] values = {3, 4, 1, 5, 20, 1, 3, 3, 4, 6};
System.out.println(IntStream.of(values).distinct().filter(e -> e > 3 && e % 2 == 0).average().getAsDouble());
System.out.println("The sum of the first 4 numbers is " + IntStream.of(values).limit(4).sum());
IntSummaryStatistics stats = IntStream.of(values).summaryStatistics();
System.out.println(stats.getMax());
System.out.println(stats.getSum());
System.out.println(stats.getMin());
System.out.println(stats.getAverage());
System.out.println(stats.getCount());
String[] names = {"John", "Peter", "Susan", "Kim", "Jen", "George", "Alan", "Stacy", "Michelle",
"john"};
System.out.println(Stream.of(names).mapToInt(e -> e.length()).sum());
System.out.println("The number of digits in array values is " +
Stream.of(values).map(e -> e + "")
.mapToInt(e -> e.length()).sum());
}
}