forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreams6.java
More file actions
99 lines (87 loc) · 2.44 KB
/
Streams6.java
File metadata and controls
99 lines (87 loc) · 2.44 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.winterbe.java8;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* @author Benjamin Winterberg
*/
public class Streams6 {
public static void main(String[] args) throws IOException {
// test1();
// test2();
// test3();
// test4();
// test5();
// test6();
// test7();
test8();
}
private static void test8() throws IOException {
Path start = Paths.get("/Users/benny/Documents");
int maxDepth = 5;
long fileCount =
Files
.walk(start, maxDepth)
.filter(path -> String.valueOf(path).endsWith("xls"))
.count();
System.out.format("XLS files found: %s", fileCount);
}
private static void test7() throws IOException {
Path start = Paths.get("/Users/benny/Documents");
int maxDepth = 5;
Files
.find(start, maxDepth, (path, attr) ->
String.valueOf(path).endsWith("xls"))
.sorted()
.forEach(System.out::println);
}
private static void test6() throws IOException {
Files
.list(Paths.get("/usr"))
.sorted()
.forEach(System.out::println);
}
private static void test5() throws IOException {
Files
.lines(Paths.get("/foo", "bar"))
.filter(line -> line.startsWith("A"))
.forEach(System.out::println);
}
private static void test4() {
Stream
.of(new BigDecimal("1.2"), new BigDecimal("3.7"))
.mapToDouble(BigDecimal::doubleValue)
.average()
.ifPresent(System.out::println);
}
private static void test3() {
IntStream
.range(0, 10)
.average()
.ifPresent(System.out::println);
}
private static void test2() {
IntStream
.builder()
.add(1)
.add(3)
.add(5)
.add(7)
.add(11)
.build()
.average()
.ifPresent(System.out::println);
}
private static void test1() {
int[] ints = {1, 3, 5, 7, 11};
Arrays
.stream(ints)
.average()
.ifPresent(System.out::println);
}
}