Java 11 新特性
Java 11 是 Java 8 之后的第一个长期支持版本 (long term suppoert LTS),Oracle 将在 2019年1月停止支持 Java 8.
Oracle VS Open JDK
Java 10 是最后一个免许可商用版本,如果不需要 Oracle 商业支持的话,可以选择 Open JDK
直接运行 java 文件
Java 11 之前需要先用 javac 把 java 编译为 class 文件才能执行
Java 11 可以直接执行 java 文件
PS C:\Users\lingh\Desktop> java hello.java
hello java
PS C:\Users\lingh\Desktop>
String 一些新方法
Java 11 String 增加了一些方法: strip, stripLeading, stripTrailing, isBlank, lines, repeat
有些这些方法后可以更方便进行 String 操作
在 JDK 源码里搜索 @since 11 可以知道 Java 11 String 增加了哪些方法
/**
* Returns a string whose value is this string, with all leading
* and trailing {@link Character#isWhitespace(int) white space}
* removed.
* <p>
* If this {@code String} object represents an empty string,
* or if all code points in this string are
* {@link Character#isWhitespace(int) white space}, then an empty string
* is returned.
* <p>
* Otherwise, returns a substring of this string beginning with the first
* code point that is not a {@link Character#isWhitespace(int) white space}
* up to and including the last code point that is not a
* {@link Character#isWhitespace(int) white space}.
* <p>
* This method may be used to strip
* {@link Character#isWhitespace(int) white space} from
* the beginning and end of a string.
*
* @return a string whose value is this string, with all leading
* and trailing white space removed
*
* @see Character#isWhitespace(int)
*
* @since 11
*/
public String strip() {
String ret = isLatin1() ? StringLatin1.strip(value)
: StringUTF16.strip(value);
return ret == null ? this : ret;
}
Files 一些新方法
java.nio.file.Files 增加了 readString 和 writeString 方法
@Test
public void testFilesNewMethods() throws IOException {
String text = "Files 一些新方法";
Path path = Files.writeString(Files.createTempFile("FilesNewMethods","txt"),text, StandardCharsets.UTF_8);
String readText = Files.readString(path);
Files.delete(path);
Assert.assertEquals(readText, text);
}
Collection 转 Array
java.util.Collection 增加了 toArray 默认方法,方便把集合转化为数组
default <T> T[] toArray(IntFunction<T[]> generator) {
return toArray(generator.apply(0));
}
@Test
public void testCollectionToArray() {
List<String> books = Arrays.asList("java", "spring");
String[] array = books.toArray(String[]::new);
Assert.assertTrue(array[0].equals("java") && array[1].equals("spring"));
}
Predicate not 新方法
java.util.function.Predicate 增加了 not 新方法
static <T> Predicate<T> not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return (Predicate<T>)target.negate();
}
@Test
public void testNot() {
List<String> books = Arrays.asList("java", "", " ", "spring");
List<String> filteredBooks = books.stream().filter(Predicate.not(String::isBlank)).collect(Collectors.toList());
Assert.assertTrue(filteredBooks.size() == 2 && filteredBooks.get(0).equals("java") && filteredBooks.get(1).equals("spring"));
}
Http Client
Java 9 引入了 Http Client, 在 Java 11 变成了标准特性
@Test
public void testGet() throws IOException, InterruptedException {
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(20))
.build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("https://www.baidu.com"))
.build();
HttpResponse<String> httpResponse = httpClient.send(httpRequest,HttpResponse.BodyHandlers.ofString());
Assert.assertTrue(httpResponse.statusCode() == 200 && httpResponsebody().contains("关于百度"));
}
标签:11,Files,java,String,特性,Java,string
From: https://www.cnblogs.com/goallin/p/17614375.html