你好啊,我是大阳,本文给你介绍Java11的新个性,并提供一些代码示例。
本文次要内容
- HTTP客户端API
- 无需编译即可启动单文件程序
- 字符串API更新
- Collection.toArray
- Files.readString() 和 Files.writeString()
- Optional.isEmpty()
Java 11(2018 年 9 月公布)蕴含许多重要且有用的更新。让咱们看看它为开发人员和架构师带来的新性能和改良。
1. HTTP客户端API
Java 应用HttpURLConnection
进行HTTP通信曾经很长一段时间了。但随着工夫的推移,要求变得越来越简单,应用程序的要求也越来越高。在 Java 11 之前,开发人员不得不求助于功能丰富的库,如Apache HttpComponents或OkHttp等。
咱们看到Java 9公布蕴含一个HttpClient
实现作为试验性功能。它随着工夫的推移而倒退,当初是 Java 11 的最终性能。当初 Java 应用程序能够进行 HTTP 通信,而无需任何内部依赖。
1.1 如何应用HttpClient
java.net.http
模块和典型 HTTP 交互如下所示 :
- 创立HttpClient的实例并依据须要进行配置。
- 创立一个HttpRequest实例并填充信息。
- 将申请传递给客户端,执行申请并检索HttpResponse的实例。
- 解决蕴含在
HttpResponse
中的信息.
HTTP API 能够解决同步和异步通信。让咱们看一个简略的例子。
1.2 同步申请示例
请留神 http 客户端 API 如何应用构建器模式来创立简单对象。
package cn.dayangshuo.http;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
/**
* @author DAYANG
*/
public class HttpClientTest {
public static void main(String[] args) {
HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
try {
String urlEndpoint = "https://www.baidu.com/s";
URI uri = URI.create(urlEndpoint + "?wd=java11");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.build();
HttpResponse<String> response = httpClient.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status code: " + response.statusCode());
System.out.println("Headers: " + response.headers().allValues("content-type"));
System.out.println("Body: " + response.body());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
1.3 异步申请示例
如果咱们不想期待响应,异步通信很有用。咱们提供回调处理程序,当响应可用时执行。
留神应用sendAsync()办法发送异步申请。
package cn.dayangshuo.http;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
/**
* @author DAYANG
*/
public class HttpClientTest {
public static void main(String[] args) {
final List<URI> uris = Stream.of(
"https://www.baidu.com/",
"https://www.zhihu.com/people/da-yang-12-48",
"https://dayangshuo.cn"
).map(URI::create).collect(toList());
HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
//回调设置
CompletableFuture[] futures = uris.stream()
.map(uri -> verifyUri(httpClient, uri))
.toArray(CompletableFuture[]::new);
CompletableFuture.allOf(futures).join();
}
private static CompletableFuture<Void> verifyUri(HttpClient httpClient, URI uri) {
HttpRequest request = HttpRequest.newBuilder()
.timeout(Duration.ofSeconds(5))
.uri(uri)
.build();
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::statusCode)
.thenApply(statusCode -> statusCode == 200)
.exceptionally(ex -> false)
.thenAccept(valid -> {
if (valid) {
System.out.println("[SUCCESS] Verified " + uri);
} else {
System.out.println("[FAILURE] Could not " + "verify " + uri);
}
});
}
}
2. 无需编译即可启动单文件程序
传统上,对于咱们想要执行的每个程序,咱们都须要先编译它。对于用于测试目标的小程序来说,这仿佛是不必要的简短过程。
Java 11 扭转了它,当初咱们能够执行蕴含在单个文件中的 Java 源代码,而无需先编译它。
编写一个简略的HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
要执行上述类,请间接应用java
命令运行它:
//控制台
$ java HelloWorld.java
Hello World!
请留神,该程序不能应用任何内部依赖除了应用java.base module模块化. 并且程序只能是单文件程序。
3. 字符串API更新
3.1 String.repeat(Integer)
此办法只是反复一个字符串n
次。它返回一个字符串,其值为反复 N 次的给定字符串的串联。
如果此字符串为空或计数为零,则返回空字符串。
public class HelloWorld {
public static void main(String[] args) {
String str = "1".repeat(5);
//打印出:11111
System.out.println(str);
}
}
3.2. String.isBlank()
此办法批示字符串是空的还是仅蕴含空格。以前,咱们始终在用 Apache 的StringUtils.java
.
public class HelloWorld {
public static void main(String[] args) {
"1".isBlank(); //false
"".isBlank(); //true
" ".isBlank(); //true
}
}
3.3. String.strip()
此办法负责删除头和尾空格。
咱们能够通过应用String.stripLeading()仅删除头字符,应用 String.stripTrailing ( )仅删除尾字符。
public class HelloWorld {
public static void main(String[] args) {
" hi ".strip(); //"hi"
" hi ".stripLeading(); //"hi "
" hi ".stripTrailing(); //" hi"
}
}
3.4. String.lines()
此办法有助于将多行文本作为Stream解决。
public class HelloWorld {
public static void main(String[] args) {
String testString = "hello\nworld\nis\nexecuted";
List<String> lines = new ArrayList<>();
testString.lines().forEach(line -> lines.add(line));
assertEquals(List.of("hello", "world", "is", "executed"), lines);
}
}
4. Collection.toArray
在 Java 11 之前,将汇合转换为数组并不简略。Java 11 使转换更加不便。
public class HelloWorld {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("alex");
names.add("brian");
names.add("charles");
String[] namesArr1 = names.toArray(new String[names.size()]); //Java 11之前
String[] namesArr2 = names.toArray(String[]::new); //Java 11
}
}
5. Files.readString() 和 Files.writeString()
应用这些重载办法,Java 11 旨在缩小大量样板代码,从而更容易读取和写入文件。
public class HelloWorld {
public static void main(String[] args) {
//读取文件内容为字符串
URI txtFileUri = getClass().getClassLoader().getResource("helloworld.txt").toURI();
String content = Files.readString(Path.of(txtFileUri),Charset.defaultCharset());
//把字符串写入文件
Path tmpFilePath = Path.of(File.createTempFile("tempFile", ".tmp").toURI());
Path returnedFilePath = Files.writeString(tmpFilePath,"Hello World!",
Charset.defaultCharset(), StandardOpenOption.WRITE);
}
}
6. Optional.isEmpty()
Optional是一个容器对象,它可能蕴含也可能不蕴含非空值。如果不存在任何值,则该对象被认为是空的。
isPresent()办法如果值存在则返回true,否则返回false。
isEmpty()办法与isPresent()办法相同,如果存在值则返回false,否则返回true。
所以咱们无论如何都不要写否定条件。适当时应用这两种办法中的任何一种。
public class HelloWorld {
public static void main(String[] args) {
String currentTime = null;
assertTrue(!Optional.ofNullable(currentTime).isPresent());
assertTrue(Optional.ofNullable(currentTime).isEmpty());
currentTime = "12:00 PM";
assertFalse(!Optional.ofNullable(currentTime).isPresent());
assertFalse(Optional.ofNullable(currentTime).isEmpty());
}
}
发表回复