Java SE 11也被命名为18.9(基于使用发布年份和月份的新命名方案),预计将在9月的最后一周发布GA。 频繁发布新JDK版本的新方法是允许语言创建者引入新功能,并向开发人员社区更快地进行API更新。

通常,API更新不会引起注意,并且会被一些重大更改所掩盖。 因此,我想到了枚举Java 11中可能没有的Java 10中的一些API更改。

我正在使用从此处下载的jdk-11-ea + 16构建

Character.toString(int)

此方法返回给定Unicode代码点的字符串表示形式,如下所示:

jshell> Character.toString(100)
$10 ==> "d"
jshell> Character.toString(66)
$7 ==> "B"

CharacterSequence.compare(java.lang.CharSequence,java.lang.CharSequence)

这将按字典顺序比较两个字符序列,如果第一个字符序列在字典上小于或等于第二个,则返回负,零或正。

词典上的意思是字典顺序或字母顺序。

jshell> CharSequence.compare("girl", "boy")
$12 ==> 5

jshell> CharSequence.compare("girl", "girl")
$13 ==> 0

jshell> CharSequence.compare("hello", "world")
$14 ==> -15

重复(int)

jshell> "**".repeat(5)
$15 ==> "**********"

jshell> "**".repeat(-7)
|  Exception java.lang.IllegalArgumentException: count is negative: -7
|        at String.repeat (String.java:3147)
|        at (#16:1)

jshell> "**".repeat(0)
$17 ==> ""

jshell> "**".repeat(1)
$18 ==> "**"

isBlank()

jshell> String msg = "hello"
msg ==> "hello"

jshell> msg.isBlank()
$22 ==> false

jshell> String msg = ""
msg ==> ""

jshell> msg.isBlank()
$24 ==> true

jshell> String msg = " "
msg ==> " "

jshell> msg.isBlank()
$26 ==> true

strip(),stripTrailing(),stripLeading()

jshell> " hello world ".strip()
$29 ==> "hello world"

jshell> "hello world    ".strip()
$30 ==> "hello world"

jshell> "hello world    ".stripTrailing()
$31 ==> "hello world"

jshell> "        hello world    ".stripLeading()
$32 ==> "hello world    "

jshell> "    ".strip()
$33 ==> ""

lines()

jshell> String content =  "this is a multiline content\nMostly obtained from some file\rwhich we will break into lines\r\nusing the new api"
content ==> "this is a multiline content\nMostly obtained fro ... ines\r\nusing the new api"

jshell> content.lines()
$36 ==> java.util.stream.ReferencePipeline$Head@5ec0a365

jshell> content.lines().forEach(System.out::println)
this is a multiline content
Mostly obtained from some file
which we will break into lines
using the new api

java.nio.file.Path.of()

在此版本之前,有没有在工厂方法java.nio.file.Path ,虽然有一个方法java.nio.file.Paths 。 此版本在java.nio.file.Path中引入了一种工厂方法,该方法有两个变体:
1.将String放置到资源中
2.将URI位置带到资源 两者都显示如下:

jshell> Path uriPath = Path.of(new URI("file:///C:/Program%20Files/Java/jdk-11/release"))
uriPath ==> C:\Program Files\Java\jdk-11\release

jshell> Files.readAllLines(uriPath).forEach(System.out::println)

jshell> Path filePath = Path.of("..", "release")
filePath ==> ..\release

jshell> Files.readAllLines(filePath).forEach(System.out::println)

Pattern.asMatchPredicate()

此API返回java.util.function.Predicate ,可用于测试给定的字符串是否与使用java.util.regex.Pattern编译的模式匹配

jshell> Pattern somePattern =  Pattern.compile("\\w+@\\w+[.]com")
somePattern ==> \w+@\w+[.]com

jshell> Predicate<String> somePredicate = somePattern.asMatchPredicate()
somePredicate ==> java.util.regex.Pattern$$Lambda$26/0x00000008000d0840@34c4973

jshell> somePredicate.test("sana@gmail.net")
$55 ==> false

jshell> somePredicate.test("sana@gmail.com")
$56 ==> true

jshell> somePredicate.test("sana#@gmail.com")
$57 ==> false

与Java EE相关的API,即Corba,JAXB,JAX WS(Web服务)已被删除。 直到Java 10从孵化器移到其自己的模块java.net.http之前,孵化器中一直使用的HTTP客户端库。 我很快会在新的HTTP客户端上写一些帖子。

翻译自: https://www.javacodegeeks.com/2018/06/api-updates-java.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐