您现在的位置是:首页 > 文章详情

JDK 11 已处于特性冻结状态,看看 Java 11 API 变更提案

日期:2018-07-24点击:738

自从上个月进入“减速(ramp-down)”阶段以来,JDK 11 的特性已经处于冻结状态。这些重大的变化已被列为 JEP(JDK Enhancement Proposal 特性增强提议)。此外,JDK 11 中也有很多除 JEP 之外的变化,但官方尚未总结。因此,本文将列出我所知道的 JDK 11 中的 API 变更。

String

lines()

字符串实例方法,使用专门的 Spliterator 来懒惰地提供源字符串中的行

jshell> "test\nhoge\n".lines().map(String::toUpperCase).toArray() $11 ==> Object[2] { "TEST", "HOGE" }

repeat(int)

按照参数 int 提供的次数来重复字符串的运行次数

jshell> "test".repeat(3) $7 ==> "testtesttest"

isBlank()

验证当前字符串是否为空,或者是否只包括空白字符(空白字符由 Character.isWhiteSpace(int) 验证)

jshell> var halfSpace = "\u0020" halfSpace ==> " " jshell> halfSpace.isBlank() $11 ==> true jshell> var fullSpace = "\u3000" fullSpace ==> " " jshell> fullSpace.isBlank() $13 ==> true

strip()/stripLeading()/stripTrailing()

这三个方法的作用分别是去掉字符串头和尾的空白符、字符串头的空白符、字符串尾的空白符,基本与 trim()/trimLeft()/trimRight() 方法相同,不过它们的空白字符由 Character.isWhiteSpace(int) 验证

jshell> var aaa = fullSpace + "aaa" + fullSpace aaa ==> " aaa " jshell> aaa.strip() $14 ==> "aaa" jshell> aaa.trim() $15 ==> " aaa "

CharSequence

compare(CharSequence, CharSequence)

按字典顺序进行排序

它被 CharSequence/StringBuffer/StringBuilder 中的 compareTo() 使用。因此,这三个类都实现了 Comparable。

Character

toString(int)

JDK 11 使这个过程变得更加方便

JDK10.0.1

jshell> Character.toString(65) |  Error: |  incompatible types: possible lossy conversion from int to char |  Character.toString(65) |

JDK11ea14

jshell> Character.toString(65) $9 ==> "A"

Path

of(String, String...)

此前我们需要使用 Paths.get()。现在,我们像其他类一样使用 of()。

Files

writeString(Path, CharSequence)

我们可以使用该方法来保存一个 String 字符串。

jshell> Files.writeString(Path.of("test.txt"), "Hello!!!") $3 ==> test.txt

readString(Path)

我们可以使用该方法来读取一个 String 字符串。

jshell> Files.readString(Path.of("test.txt")) $4 ==> "Hello!!!"

Reader

nullReader()

使用该方法,我们可以得到一个不执行任何操作的 Reader。

Writer

nullWriter()

使用该方法,我们可以得到一个不执行任何操作的 Writer。

InputStream

nullInputStream()

使用该方法,我们可以得到一个不执行任何操作的 InputStream。

OutputStream

nullOutputStream()

使用该方法,我们可以得到一个不执行任何操作的 OutputStream。

Predicate

not(Predicate)

此前在需要反转条件的地方,我们选择不使用方法引用。现在相反,我们可以使用方法引用。

jshell> Stream.of("aa", "", "bb").filter(Predicate.not(String::isEmpty)).toArray() $23 ==> Object[2] { "aa", "bb" }

Collection

toArray(IntFunction)

此前,我们需要使用像 list.toArray(new String[list.size())]) 这样的无风格标记(non-stylish notation)来从一个集合创建一个类型化数组。现在,我们可以以风格标记(stylish notation)的方式进行编写。

jshell> List.of("aa","bb").toArray(String[]::new) $1 ==> String[2] { "aa", "bb" }

Optional/OptionalInt/OptionalLong/OptionalDouble

isEmpty()

isPresent() 方法此前已经存在,现在我们使用 isEmpty() 方法。

jshell> Optional.ofNullable(null).isEmpty() $5 ==> true

TimeUnit

convert(Duration)

该方法已经添加到 java.util.concurrent.TimeUnit 中。

Pattern

asMatchPredicate()

到目前为止,只有 asPredicate() 方法,但现在我们还拥有 asMatchPredicate() 方法。

jshell> var pred = Pattern.compile("aaa").asPredicate() pred ==> java.util.regex.Pattern$Lambda$25/0x00000008000b5040@2f686d1f jshell> pred.test("aaa") $6 ==> true jshell> pred.test("aaab") $7 ==> true jshell> var matPred = Pattern.compile("aaa").asMatchPredicate() matP ==> java.util.regex.Pattern$Lambda$24/0x00000008000b6440@402a079c jshell> matPred.test("aaa") $9 ==> true jshell> matPred.test("aaab") $10 ==> false

ListSelectionModel

已添加 getSelectedIndices() / getSelectedCount() 方法

Thread

destroy()/stop(Throwable)

移除 destroy() 方法,保留 stop() 方法。

Policy

已移除 javax.security.auth.Policy。

ArrayIndexOutOfBoundsException

抛出的异常信息已修改:

JDK10.0.1

jshell> new int[]{}[0] |  java.lang.ArrayIndexOutOfBoundsException thrown: 0 |        at (#8:1)

JDK11ea14

jshell> new int[]{}[0] |  Exception java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 |        at (#4:1)

IndexOutOfBoundsException

在本次变更中,已在异常信息中移除 hyphens。

JDK10.0.1

jshell> List.of().get(0) |  java.lang.IndexOutOfBoundsException thrown: Index 0 out-of-bounds for length 0 |        at Preconditions.outOfBounds (Preconditions.java:64) |        at Preconditions.outOfBoundsCheckIndex (Preconditions.java:70) |        at Preconditions.checkIndex (Preconditions.java:248) |        at Objects.checkIndex (Objects.java:372) |        at ImmutableCollections$List0.get (ImmutableCollections.java:106) |        at (#6:1)

JDK11ea14

jshell> List.of().get(0) |  Exception java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 |        at ImmutableCollections$ListN.get (ImmutableCollections.java:411) |        at (#3:1)

System

arraycopy

JDK10

jshell> System.arraycopy(new int[0],0,new double[0],0,0) |  java.lang.ArrayStoreException thrown

JDK11ea19

jshell> System.arraycopy(new int[0], 0, new double[0], 0, 0) |  Exception java.lang.ArrayStoreException: arraycopy: type mismatch: can not copy int[] into double[]

setProperty(String, String)

之前改变 java.home 会导致一些问题,现在问题已得到解决。

支持 Japanese New Era

Japanese Imperial Era 计划于 2019.5.1 改用新的规则。

本次变更是作为 NewEra 占位符引入的。

在日本政府宣布之后,它将在 JDK 12.0.1 中进行更新。

JDK10.0.1

jshell> JapaneseDate.of(2019, 5, 1) $15 ==> Japanese Heisei 31-05-01

JDK11 ea18

jshell> JapaneseDate.of(2019, 5, 1) $3 ==> Japanese NewEra 1-05-01

目前我们还未能将 May 1st Heisei 31 作为我们的 JapaneseDate。

JDK10.0.1

jshell> JapaneseDate.of(JapaneseEra.HEISEI, 31, 5, 1) $14 ==> Japanese Heisei 31-05-01

JDK11 ea18

jshell> JapaneseDate.of(JapaneseEra.HEISEI, 31, 5, 1) |  Exception java.time.DateTimeException: year, month, and day not valid for Era |        at JapaneseDate.of (JapaneseDate.java:231) |        at (#2:1)

Base64

从 ea20 起,使用 AVX512 进行编码会变得更快,但在 Windows 上无法确定。

Boolean

parseBoolean

官方表示,在删除冗余的空检查后,它的速度变得更快。

JDK10

public static boolean parseBoolean(String s) {         return ((s != null) && s.equalsIgnoreCase("true"));     }

JDK11

public static boolean parseBoolean(String s) {     return "true".equalsIgnoreCase(s); }

还未确定是否存在性能差异。

TimSort

TimSort 是用于 Array.sort() 和 Collection.sort() 的主要算法。

但它有一个错误,主要发生在为某些序列抛出一个 ArrayIndexOutOfBoundsException 异常,不过似乎已修复,尚未确定。

来源:https://dzone.com/ 编译:开源中国

原文链接:https://www.oschina.net/news/98314/java-11-api-changes-so-far
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章