java 11 新特性学习

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

1. String API

字符串绝对是 Java 中最常用的一个类了String 类的方法使用率也都非常的高在 Java 11 中又为 String 类带来了一系列的好用操作。

  1. isBlank() 判空。 // 判空blank里我放入了全角空格半角空格TAB String blank = "   "; System.out.println(blank.isBlank()); // 输出 // true
  2. lines() 分割获取字符串流。 // lines 返回一个 Stream String line = "a\nb\nc"; Stream<String> lines = line.lines(); // 使用 lambda 遍历 lines.forEach(System.out::println); // 输出 // a // b // c
  3. repeat() 复制字符串 // 复制字符串 String repeat = "我的微信:wn8398,"; String repeat3 = repeat.repeat(3); System.out.println(repeat3); // 输出 // 我的微信:wn8398,我的微信:wn8398,我的微信:wn8398,
  4. strip() 去除前后空白字符。 // 去除前后空白 String strip = "   https://www.wdbyte.com  "; System.out.println("==" + strip.trim() + "=="); // 去除前后空白字符如全角空格TAB System.out.println("==" + strip.strip() + "=="); // 去前面空白字符如全角空格TAB System.out.println("==" + strip.stripLeading() + "=="); // 去后面空白字符如全角空格TAB System.out.println("==" + strip.stripTrailing() + "=="); // 输出 // ==  https://www.wdbyte.com  == // ==https://www.wdbyte.com== // ==https://www.wdbyte.com  == // ==   https://www.wdbyte.com== 这里注意trim 只能去除半角空格而 strip去除各种空白符
// String新增了strip()方法和trim()相比strip()可以去掉Unicode空格例如中文空格

String s = " Hello, JDK11!\u3000\u3000";
System.out.println("     original: [" + s + "]");
System.out.println("         trim: [" + s.trim() + "]");
System.out.println("        strip: [" + s.strip() + "]");
System.out.println(" stripLeading: [" + s.stripLeading() + "]");
System.out.println("stripTrailing: [" + s.stripTrailing() + "]");

2. File API

读写文件变得更加方便。

// 创建临时文件
Path path = Files.writeString(Files.createTempFile("test", ".txt"), "http://www.csdn.net");
System.out.println(path);

Files.writeString(    Path.of("./", "tmp.txt"),     "hello, jdk11 files api",StandardCharsets.UTF_8); 
String s = Files.readString(    Paths.get("./tmp.txt"),  StandardCharsets.UTF_8);  

3. JEP 321 - HTTP Client

Java 11 中 Http Client API 得到了标准化的支持。且支持 HTTP/1.1 和 HTTP/2 也支持 websockets。

你可以像这样发起一个请求。

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://www.hao123.com"))
        .build();
// 异步
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
        .thenApply(HttpResponse::body)
        .thenAccept(System.out::println)
        .join();

// 同步
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

可以参考 OpenJDK 官方文档。

http://openjdk.java.net/groups/net/httpclient/recipes-incubating.html

4. JEP 323 - Lambda 局部变量推断

Java 10 中引入了 var 语法可以自动推断变量类型。在 Java 11 中这个语法糖可以在 Lambda 表达式中使用了。


public class LocalVar {
    public static void main(String[] args) {
        Arrays.asList("Java", "Python", "Ruby")
            .forEach((var s) -> {
                System.out.println("Hello, " + s);
            });
	}
}

这里需要注意的是(var k,var v) 中k 和 v 的类型要么都用 var 要么都不写要么都写正确的变量类型。而不能 var 和其他变量类型混用。

5.List API

对于List接口新增了一个of(T...)接口用于快速创建List对象

List<String> list = List.of("Java", "Python", "Ruby");

ListtoArray()还新增了一个重载方法可以更方便地把List转换为数组。可以比较一下两种转换方法

// 旧的方法:传入String[]:
String[] oldway = list.toArray(new String[list.size()]);

// 新的方法:传入IntFunction:
String[] newway = list.toArray(String[]::new);

6. 免费的飞行记录器

商业版 JDK 中一直有一款低开销的事件信息收集工具也就是飞行记录器Java Flight Recorder它可以对 JVM 进行检查、分析、记录等。当出现未知异常时可以通过记录进行故障分析。这个好用的工具在 Java 11 中将开源免费。所有人都可以使用这个功能了。

参考JDK 11

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java