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

Commitf2d7b5b

Browse files
committed
More Java 11 examples
1 parent4b92b43 commitf2d7b5b

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

‎src/com/winterbe/java11/HttpClientExamples.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,40 @@ public static void main(String[] args) throws IOException, InterruptedException
1919
}
2020

2121
privatestaticvoidsyncRequest()throwsIOException,InterruptedException {
22-
varclient =HttpClient.newHttpClient();
2322
varrequest =HttpRequest.newBuilder()
2423
.uri(URI.create("https://winterbe.com"))
2524
.build();
25+
varclient =HttpClient.newHttpClient();
2626
HttpResponse<String>response =client.send(request,HttpResponse.BodyHandlers.ofString());
2727
System.out.println(response.body());
2828
}
2929

3030
privatestaticvoidasyncRequest() {
31-
varclient =HttpClient.newHttpClient();
3231
varrequest =HttpRequest.newBuilder()
3332
.uri(URI.create("https://winterbe.com"))
3433
.build();
34+
varclient =HttpClient.newHttpClient();
3535
client.sendAsync(request,HttpResponse.BodyHandlers.ofString())
3636
.thenApply(HttpResponse::body)
3737
.thenAccept(System.out::println);
3838
}
3939

4040
privatestaticvoidpostData()throwsIOException,InterruptedException {
41-
varclient =HttpClient.newHttpClient();
4241
varrequest =HttpRequest.newBuilder()
4342
.uri(URI.create("https://postman-echo.com/post"))
4443
.timeout(Duration.ofSeconds(30))
4544
.version(HttpClient.Version.HTTP_2)
4645
.header("Content-Type","text/plain")
4746
.POST(HttpRequest.BodyPublishers.ofString("Hi there!"))
4847
.build();
48+
varclient =HttpClient.newHttpClient();
4949
varresponse =client.send(request,HttpResponse.BodyHandlers.ofString());
5050
System.out.println(response.statusCode());// 200
5151
}
5252

5353
privatestaticvoidbasicAuth()throwsIOException,InterruptedException {
5454
varclient =HttpClient.newHttpClient();
55+
5556
varrequest1 =HttpRequest.newBuilder()
5657
.uri(URI.create("https://postman-echo.com/basic-auth"))
5758
.build();

‎src/com/winterbe/java11/Misc.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
packagecom.winterbe.java11;
2+
3+
importjava.io.File;
4+
importjava.io.FileOutputStream;
5+
importjava.io.IOException;
6+
importjava.util.List;
7+
importjava.util.Map;
8+
importjava.util.Optional;
9+
importjava.util.stream.Collectors;
10+
importjava.util.stream.Stream;
11+
12+
publicclassMisc {
13+
14+
@Deprecated(forRemoval =true)
15+
Stringfoo;
16+
17+
publicstaticvoidmain(String[]args)throwsIOException {
18+
collections();
19+
strings();
20+
optionals();
21+
inputStreams();
22+
streams();
23+
}
24+
25+
privatestaticvoidstreams() {
26+
System.out.println(Stream.ofNullable(null).count());// 0
27+
System.out.println(Stream.of(1,2,3,2,1)
28+
.dropWhile(n ->n <3)
29+
.collect(Collectors.toList()));// [3, 2, 1]
30+
System.out.println(Stream.of(1,2,3,2,1)
31+
.takeWhile(n ->n <3)
32+
.collect(Collectors.toList()));// [1, 2]
33+
}
34+
35+
privatestaticvoidinputStreams()throwsIOException {
36+
varclassLoader =ClassLoader.getSystemClassLoader();
37+
varinputStream =classLoader.getResourceAsStream("com/winterbe/java11/dummy.txt");
38+
vartempFile =File.createTempFile("dummy-copy","txt");
39+
try (varoutputStream =newFileOutputStream(tempFile)) {
40+
inputStream.transferTo(outputStream);
41+
}
42+
System.out.println(tempFile.length());
43+
}
44+
45+
privatestaticvoidoptionals() {
46+
System.out.println(Optional.of("foo").orElseThrow());// foo
47+
System.out.println(Optional.ofNullable(null).or(() ->Optional.of("bar")).get());// bar
48+
System.out.println(Optional.of("foo").stream().count());// 1
49+
}
50+
51+
privatestaticvoidstrings() {
52+
System.out.println(" ".isBlank());
53+
System.out.println(" Foo Bar ".strip());// "Foo Bar"
54+
System.out.println(" Foo Bar ".stripTrailing());// " Foo Bar"
55+
System.out.println(" Foo Bar ".stripLeading());// "Foo Bar "
56+
System.out.println("Java".repeat(3));// "JavaJavaJava"
57+
System.out.println("A\nB\nC".lines().count());// 3
58+
}
59+
60+
privatestaticvoidcollections() {
61+
varlist =List.of("A","B","C");
62+
varcopy =List.copyOf(list);
63+
System.out.println(list ==copy);// true
64+
65+
varmap =Map.of("A",1,"B",2);
66+
System.out.println(map);
67+
}
68+
69+
}

‎src/com/winterbe/java11/dummy.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foobar

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp