1. 程式人生 > 其它 >Java11新特性筆記

Java11新特性筆記

Java11 是甲骨文於2018年9月釋出,是在 8 以後釋出的第一個長期支援的的版本。

String

String作為一個常用的類,在11裡面增加一些新的方法

lines()

此方法返回 Stream,作用是將一串文字,以換行符分隔並返回行流

支援的換行符有:\n\r\ r \ n 正好對應不同作業系統裡的換行符

public static void main(String[] args) {
    String str = "第一行 \n第二行 \r第三行\r\n第四行\t不換行";
    str.lines().forEach(System.out::println);
}

結果

第一行 
第二行 
第三行
第四行	不換行

isBlank()

判斷字串是否為空白,此方法使用需要和 isEmpty() 方法注意,純空格在 isBlank() 方法中為 true,在 isEmpty() 方否中為 false

public static void main(String[] args) {
    String str1 = "字 符 串";
    String str2 = "";
    String str3 = " ";
    System.out.println(">>>> isBlank 方法 <<<<");
    System.out.println(str1.isBlank());
    System.out.println(str2.isBlank());
    System.out.println(str3.isBlank());
    System.out.println(">>>> isEmpty 方法 <<<<");
    System.out.println(str1.isEmpty());
    System.out.println(str2.isEmpty());
    System.out.println(str3.isEmpty());
}

結果

>>>> isBlank 方法 <<<<
false
true
true
>>>> isEmpty 方法 <<<<
false
true
false

strip()

去除字串開頭和結尾空格,不包括字元中間空格。

strip() 方法 trim() 作用一致,區別在於 strip() 方法支援 Unicode 字符集。

strip() 方法衍生而來,還有兩個方法:

  • stripLeading():僅去除字串開頭空格
  • stripTrailing():僅去除字串結尾空格
public static void main(String[] args) {
    String str = " 人生如 逆旅,我亦 是行人。\n但願初相遇,不負有心人。 ";
    System.out.println(">>>> strip() <<<<");
    System.out.println(str.strip());
    System.out.println(">>>> trim() <<<<");
    System.out.println(str.trim());
    System.out.println(">>>> 去除開頭空格 <<<<");
    System.out.println(str.stripLeading());
    System.out.println(">>>> 去除結尾空格 <<<<");
    System.out.println(str.stripTrailing());
}

結果

>>>> strip() <<<<
人生如 逆旅,我亦 是行人。
但願初相遇,不負有心人。
>>>> trim() <<<<
人生如 逆旅,我亦 是行人。
但願初相遇,不負有心人。
>>>> 去除開頭空格 <<<<
人生如 逆旅,我亦 是行人。
但願初相遇,不負有心人。 
>>>> 去除結尾空格 <<<<
 人生如 逆旅,我亦 是行人。
但願初相遇,不負有心人。

repeat()

將字串重複 N 次

public static void main(String[] args) {
   String str = "但願初相遇,不負有心人。\r\n";
   System.out.println(str.repeat(3));
}

結果

但願初相遇,不負有心人。
但願初相遇,不負有心人。
但願初相遇,不負有心人。

Files

在11中,新增了一些檔案方法,可以方便的從檔案中讀取和寫入字串

readString() 和 writeString()

public static void main(String[] args) throws IOException {
    Path file = Files.writeString(
        Files.createTempFile("FileStudy", ".txt"), "但願初相遇,不負有心人。");
    System.out.println(Files.readString(file));

}

結果

但願初相遇,不負有心人。

Collection

新增帶有 IntFunction 引數的方法 toArray(),作用就是將集合轉為陣列時候,可指定建立對應型別的陣列

public static void main(String[] args) {
    List<String> strList = new ArrayList<>(){{
        add("1");
        add("2");
        add("3");
    }};
    String[] strCollections = strList.toArray(String[]::new);
    System.out.println(strList);
    System.out.println(Arrays.toString(strCollections));
}

結果

[1, 2, 3]
[1, 2, 3]

Lambda

增加了 var 關鍵字修飾 Lambda 中的區域性變數

public static void main(String[] args) {
    List<String> strList = new ArrayList<>(){{
        add("但願初相遇  ");
        add("   不負有心人");
    }};
    System.out.println(strList);
    String str = strList.stream()
        .map((var e) -> e.strip())
        .collect(Collectors.joining(","));
    System.out.println(str);
}

結果

[但願初相遇  ,    不負有心人]
但願初相遇,不負有心人

編譯

可直接使用 java xxx.java 編譯執行,不用使用 javac 顯示編譯檔案

其他

當然了除了上面這些在實際開發中會使用到的內容,還有其他的一些改變:https://www.baeldung.com/java-11-new-features

文章來自個人部落格:暮城留風
https://www.liaocp.cn/2021/03/java-11-new-features.html