New Features in Java 9,10,11

aSn1L.png

Java 11 is the first long-term support (LTS) release after Java 8. Oracle also stopped supporting Java 8 in January 2019. As a consequence, a lot of us will upgrade to Java 11.

作为 8 之后的 LTS 版本,让我们来快速学习一下吧

模块化

1
2
3
4
module com.baeldung.java9.modules.car {
requires com.baeldung.java9.modules.engines;
exports com.baeldung.java9.modules.car.handling;
}

模块化并不算服务端很刚需的需求,更多的可以查阅 Project Jigsaw: Module System Quick-Start Guide

全新的 HTTPClient

1
2
3
4
5
6
7
8
9
10
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(20))
.build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:" + port))
.build();
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
assertThat(httpResponse.body()).isEqualTo("Hello from the server!");

Interface 私有方法

interface 越来越像一个普通 class 了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
interface InterfaceWithPrivateMethods {

private static String staticPrivate() {
return "static private";
}

private String instancePrivate() {
return "instance private";
}

default void check() {
String result = staticPrivate();
InterfaceWithPrivateMethods pvt = new InterfaceWithPrivateMethods() {
// anonymous class
};
result = pvt.instancePrivate();
}
}}

local 变量

jdk10 中增加了一个新的关键字,减少啰嗦的类型定义。

Java 8
1
Map<Integer, String> map = new HashMap<>();
Java 11
1
var idToNameMap = new HashMap<Integer, String>();

不过限制还是很多的,只能在局部变量使用。

更多 String 操作函数

增加了 isBlank, lines, strip, stripLeading, stripTrailing, and repeat 这些函数。

Java 8
1
2
3
4
5
6
String multilineString 
= "Baeldung helps \n developers \n explore Java.";
List<String> lines = Arrays.stream(multilineString.split("\n"))
.filter(StringUtils::isBlank)
.map(String::strip)
.collect(Collectors.toList());
Java 11
1
2
3
4
5
6
String multilineString 
= "Baeldung helps \n developers \n explore Java.";
List<String> lines = multilineString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());

文件读操作

增加了 readString 和 ``writeString` 两种操作

Java 8
1
2
3
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
new FileOutputStream(Files.createTempFile(tempDir, "demo", ".txt").toFile()));
outputStreamWriter.write("Sample text");
Java 11
1
2
3
Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text");
String fileContent = Files.readString(filePath);
assertThat(fileContent).isEqualTo("Sample text");

Collection 2 Array

包含一个比较 符合直觉 的方式将 Collection 转化为 Array

Java 8
1
2
3
List sampleList = Arrays.asList("Java", "Kotlin");
String[] sampleArray =sampleList.toArray(new String[0])
assertThat(sampleArray).containsExactly("Java", "Kotlin");
Java 11
1
2
3
List sampleList = Arrays.asList("Java", "Kotlin");
String[] sampleArray = sampleList.toArray(String[]::new);
assertThat(sampleArray).containsExactly("Java", "Kotlin");

Lambda 的 局部变量 语法

Java 8
1
2
3
4
5
List<String> sampleList = Arrays.asList("Java", "Kotlin");
String resultString = sampleList.stream()
.map((@Nonnull String x) -> x.toUpperCase())
.collect(Collectors.joining(", "));
assertThat(resultString).isEqualTo("JAVA, KOTLIN");
Java 11
1
2
3
4
5
List<String> sampleList = Arrays.asList("Java", "Kotlin");
String resultString = sampleList.stream()
.map((@Nonnull var x) -> x.toUpperCase())
.collect(Collectors.joining(", "));
assertThat(resultString).isEqualTo("JAVA, KOTLIN");

全量更新列表