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

Commit f2d7b5b

Browse files
committed
More Java 11 examples
1 parent 4b92b43 commit f2d7b5b

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
private static void syncRequest() throws IOException, InterruptedException {
22-
var client = HttpClient.newHttpClient();
2322
var request = HttpRequest.newBuilder()
2423
.uri(URI.create("https://winterbe.com"))
2524
.build();
25+
var client = HttpClient.newHttpClient();
2626
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
2727
System.out.println(response.body());
2828
}
2929

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

4040
private static void postData() throws IOException, InterruptedException {
41-
var client = HttpClient.newHttpClient();
4241
var request = 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+
var client = HttpClient.newHttpClient();
4949
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
5050
System.out.println(response.statusCode()); // 200
5151
}
5252

5353
private static void basicAuth() throws IOException, InterruptedException {
5454
var client = HttpClient.newHttpClient();
55+
5556
var request1 = 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+
package com.winterbe.java11;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.Stream;
11+
12+
public class Misc {
13+
14+
@Deprecated(forRemoval = true)
15+
String foo;
16+
17+
public static void main(String[] args) throws IOException {
18+
collections();
19+
strings();
20+
optionals();
21+
inputStreams();
22+
streams();
23+
}
24+
25+
private static void streams() {
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+
private static void inputStreams() throws IOException {
36+
var classLoader = ClassLoader.getSystemClassLoader();
37+
var inputStream = classLoader.getResourceAsStream("com/winterbe/java11/dummy.txt");
38+
var tempFile = File.createTempFile("dummy-copy", "txt");
39+
try (var outputStream = new FileOutputStream(tempFile)) {
40+
inputStream.transferTo(outputStream);
41+
}
42+
System.out.println(tempFile.length());
43+
}
44+
45+
private static void optionals() {
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+
private static void strings() {
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+
private static void collections() {
61+
var list = List.of("A", "B", "C");
62+
var copy = List.copyOf(list);
63+
System.out.println(list == copy); // true
64+
65+
var map = 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)

AltStyle によって変換されたページ (->オリジナル) /