Skip to content

Commit 43128ab

Browse files
committed
Code samples
1 parent 70a38ae commit 43128ab

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

src/com/winterbe/java8/samples/misc/Files1.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.file.Path;
88
import java.nio.file.Paths;
99
import java.util.List;
10+
import java.util.stream.Collectors;
1011
import java.util.stream.Stream;
1112

1213
/**
@@ -26,25 +27,26 @@ public static void main(String[] args) throws IOException {
2627
}
2728

2829
private static void testReaderLines() throws IOException {
29-
try (BufferedReader reader =
30-
Files.newBufferedReader(Paths.get("res", "nashorn1.js"))) {
31-
long countPrints = reader.lines()
30+
Path path = Paths.get("res/nashorn1.js");
31+
try (BufferedReader reader = Files.newBufferedReader(path)) {
32+
long countPrints = reader
33+
.lines()
3234
.filter(line -> line.contains("print"))
3335
.count();
3436
System.out.println(countPrints);
3537
}
3638
}
3739

3840
private static void testWriter() throws IOException {
39-
try (BufferedWriter writer =
40-
Files.newBufferedWriter(Paths.get("res", "output.js"))) {
41+
Path path = Paths.get("res/output.js");
42+
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
4143
writer.write("print('Hello World');");
4244
}
4345
}
4446

4547
private static void testReader() throws IOException {
46-
try (BufferedReader reader =
47-
Files.newBufferedReader(Paths.get("res", "nashorn1.js"))) {
48+
Path path = Paths.get("res/nashorn1.js");
49+
try (BufferedReader reader = Files.newBufferedReader(path)) {
4850
System.out.println(reader.readLine());
4951
}
5052
}
@@ -53,10 +55,11 @@ private static void testWalk() throws IOException {
5355
Path start = Paths.get("");
5456
int maxDepth = 5;
5557
try (Stream<Path> stream = Files.walk(start, maxDepth)) {
56-
long fileCount = stream
57-
.filter(path -> String.valueOf(path).endsWith(".js"))
58-
.count();
59-
System.out.format("JS files found: %s", fileCount);
58+
String joined = stream
59+
.map(String::valueOf)
60+
.filter(path -> path.endsWith(".js"))
61+
.collect(Collectors.joining("; "));
62+
System.out.println("walk(): " + joined);
6063
}
6164
}
6265

@@ -65,26 +68,36 @@ private static void testFind() throws IOException {
6568
int maxDepth = 5;
6669
try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) ->
6770
String.valueOf(path).endsWith(".js"))) {
68-
stream.sorted().forEach(System.out::println);
71+
String joined = stream
72+
.sorted()
73+
.map(String::valueOf)
74+
.collect(Collectors.joining("; "));
75+
System.out.println("find(): " + joined);
6976
}
7077
}
7178

7279
private static void testList() throws IOException {
73-
try (Stream<Path> stream = Files.list(Paths.get("/usr"))) {
74-
stream.sorted().forEach(System.out::println);
80+
try (Stream<Path> stream = Files.list(Paths.get(""))) {
81+
String joined = stream
82+
.map(String::valueOf)
83+
.filter(path -> !path.startsWith("."))
84+
.sorted()
85+
.collect(Collectors.joining("; "));
86+
System.out.println("list(): " + joined);
7587
}
7688
}
7789

7890
private static void testLines() throws IOException {
79-
try (Stream<String> stream = Files.lines(Paths.get("res", "nashorn1.js"))) {
91+
try (Stream<String> stream = Files.lines(Paths.get("res/nashorn1.js"))) {
8092
stream
8193
.filter(line -> line.contains("print"))
94+
.map(String::trim)
8295
.forEach(System.out::println);
8396
}
8497
}
8598

8699
private static void testReadWriteLines() throws IOException {
87-
List<String> lines = Files.readAllLines(Paths.get("res", "nashorn1.js"));
100+
List<String> lines = Files.readAllLines(Paths.get("res/nashorn1.js"));
88101
lines.add("print('foobar');");
89102
Files.write(Paths.get("res", "nashorn1-modified.js"), lines);
90103
}

src/com/winterbe/java8/samples/misc/Math1.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,29 @@ private static void testUnsignedInt() {
3030
System.out.println(string2);
3131

3232
try {
33-
System.out.println(Integer.parseInt(string, 10));
33+
Integer.parseInt(string, 10);
3434
}
3535
catch (NumberFormatException e) {
36-
System.out.println("could not parse signed int of " + maxUnsignedInt);
36+
System.err.println("could not parse signed int of " + maxUnsignedInt);
3737
}
3838
}
3939

4040
private static void testMathExact() {
41+
System.out.println(Integer.MAX_VALUE);
4142
System.out.println(Integer.MAX_VALUE + 1);
4243

4344
try {
4445
Math.addExact(Integer.MAX_VALUE, 1);
4546
}
4647
catch (ArithmeticException e) {
47-
System.out.println(e.getMessage());
48+
System.err.println(e.getMessage());
4849
}
4950

5051
try {
5152
Math.toIntExact(Long.MAX_VALUE);
5253
}
53-
catch (Exception e) {
54-
System.out.println(e.getMessage());
54+
catch (ArithmeticException e) {
55+
System.err.println(e.getMessage());
5556
}
5657
}
5758
}
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.winterbe.java8.samples.misc;
22

3-
import java.util.function.Predicate;
43
import java.util.regex.Pattern;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
56

67
/**
78
* @author Benjamin Winterberg
@@ -16,29 +17,33 @@ public static void main(String[] args) {
1617
}
1718

1819
private static void testChars() {
19-
String string = "foobar";
20-
string.chars()
21-
.filter(c -> c > 100)
22-
.mapToObj(c -> (char)c)
23-
.forEach(System.out::println);
20+
String string = "foobar:foo:bar"
21+
.chars()
22+
.distinct()
23+
.mapToObj(c -> String.valueOf((char) c))
24+
.sorted()
25+
.collect(Collectors.joining());
26+
System.out.println(string);
2427
}
2528

2629
private static void testPatternSplit() {
27-
Pattern.compile(":")
30+
String string = Pattern.compile(":")
2831
.splitAsStream("foobar:foo:bar")
32+
.filter(s -> s.contains("bar"))
2933
.sorted()
30-
.forEach(System.out::println);
34+
.collect(Collectors.joining(":"));
35+
System.out.println(string);
3136
}
3237

3338
private static void testPatternPredicate() {
34-
Pattern pattern = Pattern.compile(".*123.*");
35-
Predicate<String> predicate = pattern.asPredicate();
36-
System.out.println(predicate.test("a123b"));
37-
System.out.println(predicate.test("boom"));
39+
long count = Stream.of("bob@gmail.com", "alice@hotmail.com")
40+
.filter(Pattern.compile(".*@gmail\\.com").asPredicate())
41+
.count();
42+
System.out.println(count);
3843
}
3944

4045
private static void testJoin() {
41-
String string = String.join(";", "a", "b", "c", "d");
46+
String string = String.join(":", "foobar", "foo", "bar");
4247
System.out.println(string);
4348
}
4449
}

0 commit comments

Comments
 (0)