Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43128ab

Browse files
committed
Code samples
1 parent70a38ae commit43128ab

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
importjava.nio.file.Path;
88
importjava.nio.file.Paths;
99
importjava.util.List;
10+
importjava.util.stream.Collectors;
1011
importjava.util.stream.Stream;
1112

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

2829
privatestaticvoidtestReaderLines()throwsIOException {
29-
try (BufferedReaderreader =
30-
Files.newBufferedReader(Paths.get("res","nashorn1.js"))) {
31-
longcountPrints =reader.lines()
30+
Pathpath =Paths.get("res/nashorn1.js");
31+
try (BufferedReaderreader =Files.newBufferedReader(path)) {
32+
longcountPrints =reader
33+
.lines()
3234
.filter(line ->line.contains("print"))
3335
.count();
3436
System.out.println(countPrints);
3537
}
3638
}
3739

3840
privatestaticvoidtestWriter()throwsIOException {
39-
try (BufferedWriterwriter =
40-
Files.newBufferedWriter(Paths.get("res","output.js"))) {
41+
Pathpath =Paths.get("res/output.js");
42+
try (BufferedWriterwriter =Files.newBufferedWriter(path)) {
4143
writer.write("print('Hello World');");
4244
}
4345
}
4446

4547
privatestaticvoidtestReader()throwsIOException {
46-
try (BufferedReaderreader =
47-
Files.newBufferedReader(Paths.get("res","nashorn1.js"))) {
48+
Pathpath =Paths.get("res/nashorn1.js");
49+
try (BufferedReaderreader =Files.newBufferedReader(path)) {
4850
System.out.println(reader.readLine());
4951
}
5052
}
@@ -53,10 +55,11 @@ private static void testWalk() throws IOException {
5355
Pathstart =Paths.get("");
5456
intmaxDepth =5;
5557
try (Stream<Path>stream =Files.walk(start,maxDepth)) {
56-
longfileCount =stream
57-
.filter(path ->String.valueOf(path).endsWith(".js"))
58-
.count();
59-
System.out.format("JS files found: %s",fileCount);
58+
Stringjoined =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
intmaxDepth =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+
Stringjoined =stream
72+
.sorted()
73+
.map(String::valueOf)
74+
.collect(Collectors.joining("; "));
75+
System.out.println("find(): " +joined);
6976
}
7077
}
7178

7279
privatestaticvoidtestList()throwsIOException {
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+
Stringjoined =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
privatestaticvoidtestLines()throwsIOException {
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
privatestaticvoidtestReadWriteLines()throwsIOException {
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 (NumberFormatExceptione) {
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
privatestaticvoidtestMathExact() {
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 (ArithmeticExceptione) {
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 (Exceptione) {
54-
System.out.println(e.getMessage());
54+
catch (ArithmeticExceptione) {
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
packagecom.winterbe.java8.samples.misc;
22

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

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

1819
privatestaticvoidtestChars() {
19-
Stringstring ="foobar";
20-
string.chars()
21-
.filter(c ->c >100)
22-
.mapToObj(c -> (char)c)
23-
.forEach(System.out::println);
20+
Stringstring ="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
privatestaticvoidtestPatternSplit() {
27-
Pattern.compile(":")
30+
Stringstring =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
privatestaticvoidtestPatternPredicate() {
34-
Patternpattern =Pattern.compile(".*123.*");
35-
Predicate<String>predicate =pattern.asPredicate();
36-
System.out.println(predicate.test("a123b"));
37-
System.out.println(predicate.test("boom"));
39+
longcount =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
privatestaticvoidtestJoin() {
41-
Stringstring =String.join(";","a","b","c","d");
46+
Stringstring =String.join(":","foobar","foo","bar");
4247
System.out.println(string);
4348
}
4449
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp