Java 11 新特性

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("关于百度"));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值