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

栏目: IT资讯 · 发布时间: 5年前

内容简介:自从上个月进入“减速(ramp-down)”阶段以来,JDK 11 的特性已经处于冻结状态。这些重大的变化已被列为 JEP(JDK Enhancement Proposal 特性增强提议)。此外,JDK 11 中也有很多除 JEP 之外的变化,但官方尚未总结...

自从上个月进入“减速(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/ 编译:开源中国


【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上所述就是小编给大家介绍的《JDK 11 已处于特性冻结状态,看看 Java 11 API 变更提案》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

软技能

软技能

John Sonmez / 王小刚 / 人民邮电出版社 / 2016-7 / 59.00元

这是一本真正从“人”(而非技术也非管理)的角度关注软件开发人员自身发展的书。书中论述的内容既涉及生活习惯,又包括思维方式,凸显技术中“人”的因素,全面讲解软件行业从业人员所需知道的所有“软技能”。本书聚焦于软件开发人员生活的方方面面,从揭秘面试的流程到精耕细作出一份杀手级简历,从创建大受欢迎的博客到打造你,从提高自己工作效率到与如何与“拖延症”做斗争,甚至包括如何投资不动产,如何关注自己的健康。本......一起来看看 《软技能》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具