-
Notifications
You must be signed in to change notification settings - Fork 11
/
StreamWorkingWithPrimitives.java
48 lines (40 loc) · 1.32 KB
/
StreamWorkingWithPrimitives.java
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
package functionalProgramming;
import java.util.IntSummaryStatistics;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
*
* @author chengfeili
* Jun 8, 2017 10:52:06 PM
*
*/
public class StreamWorkingWithPrimitives {
public void test() {
DoubleStream oneValue = DoubleStream.of(3.14);
DoubleStream varargs = DoubleStream.of(1.0, 1.1, 1.2);
oneValue.forEach(System.out::println);
System.out.println();
varargs.forEach(System.out::println);
IntStream range = IntStream.range(1, 6);
range.forEach(System.out::println); // 1, 2, 3, 4, 5
IntStream rangeClosed = IntStream.rangeClosed(1, 5);
rangeClosed.forEach(System.out::println); // 1, 2, 3, 4, 5
Stream<String> objStream = Stream.of("penguin", "fish");
IntStream intStream = objStream.mapToInt(s -> s.length());
intStream.forEach(System.out::print); // 74
}
public void range() {
IntStream range = IntStream.range(1, 6); // 1, 2, 3, 4, 5
IntSummaryStatistics stats = range.summaryStatistics();
if (stats.getCount() == 0)
throw new RuntimeException();
int res = stats.getMax() - stats.getMin();
System.out.println("range: " + res); // 5 - 1 = 4
}
public static void main(String[] args) {
StreamWorkingWithPrimitives s = new StreamWorkingWithPrimitives();
s.test();
s.range();
}
}