JDK11新增功能
1.集合增强
集合类如List、Set和Map新增了一些方法,例如List.of可以快速创建一个不可变的List。新的集合方法使得创建不可变集合变得更加简单,也减少了修改原始集合时的副作用。
2 var局部变量关键字
3 字符串处理
String类新增了实用的方法,例如.isBlank()、.strip()、.stripTrailing()和.stripLeading(),这些方法简化了空白字符处理和行数统计
.strip()用于去除字符串首尾空白字符(空白字符:空格、tab、换行)以及Unicode字符(trim可以去除字符串首尾空白字符,但不能去掉Unicode字符--Character c = ‘\u2000’就是一种Unicode字符)
.stripTrailing()用于去除字符串尾部空白字符以及Unicode字符
.stripLeading()用于去除字符串头部空白字符以及Unicode字符
4 集合提供快速copyOf()
var list=List.of("a","b","c"); var newList=List.copyOf(list);
5 stream 扩展 takeWhile dropWhile
takeWhile()从集合获取满足条件的元素,直到不满足条件为止
dropWhile()从集合删除满足条件的元素,直到不满足条件为止
var list=List.of("zhang","li","wang"); var takeList=list.stream() .takeWhile(name->name.equals("li")) .collect(Collectors.toList()); var dropList=list.stream() .dropWhile(name->name.equals("li")) .collect(Collectors.toList());
JDK17新增功能
https://www.cnblogs.com/binbingg/p/18247201
1 文本块 -- 去除换行符
JDK 8 public static final String getHtmlJDK8() { return "<html>\n" + " <body>\n" + " <p>Hello, world</p>\n" + " </body>\n" + "</html>"; } JDK17 public static final String getHtmlJDK17() { return """ <html> <body> <p>Hello, world</p> </body> </html> """; }
2 集合类工厂方法
JDK8 Set<String> set = new HashSet<>(); set.add("a"); set.add("b"); set.add("c"); JDK17 Set<String> set=Set.of("a","b","c");
3 空指针判断
JDK17前只是会报空指针异常错误,但JDK17中会具体到哪个函数报的空指针
public static void main(String[] args) { try { //简单的空指针 String str = null; str.length(); } catch (Exception e) { e.printStackTrace(); } try { //复杂一点的空指针 var arr = List.of(null); String str = (String)arr.get(0); str.length(); } catch (Exception e) { e.printStackTrace(); } }

4 Records
针对POJO对象,Java17引入了标准解决方案:Records。它通过简洁的语法定义数据类,大大简化了POJO类的编写
Records 自动提供:
-
Getter方法(字段名即为方法名,如
name()) -
equals() 和 hashCode()
-
toString()
-
构造方法
不可变性保证
-
所有字段默认是
final的,提供不可变性 -
提供了一种简洁的方式创建不可变对象
-
线程安全的基础保证
不需要显式声明serialVersionUID
由于不可变性和确定性,JVM可以做更多优化
// 传统Java类 public final class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } // getters, equals(), hashCode(), toString()... } // 使用Record public record Person(String name, int age) {}
5 模式匹配 instanceof
模式匹配增强了instanceof功能,适用类型检查和类型转换更方便。JDK17前,使用instanceof需要配合类型转换
JDK8 public void matchByJDK8(Object value) { if (value instanceof String) { String v = (String)value; System.out.println("遇到一个String类型" + v.toUpperCase()); } else if (value instanceof Integer) { Integer v = (Integer)value; System.out.println("遇到一个整型类型" + v.longValue()); } } JDK17 public void matchByJDK17(Object value) { if (value instanceof String v) { System.out.println("遇到一个String类型" + v.toUpperCase()); //JDK17使用instanceof判断value是String类型后可以直接将其当成String类型使用而不再需要强制转换 } else if (value instanceof Integer v) { System.out.println("遇到一个整型类型" + v.longValue()); } }
6 全新的httpclient
原来的JDK自带的Http客户端真的非常难用,这也就给了很多像okhttp、restTemplate、Apache的HttpClient和feign这样的第三方库极大的发挥空间,几乎就没有人愿意去用原生的Http客户端的。但现在不一样了,感觉像是新时代的API了。FluentAPI风格,处处充满了现代风格,用起来也非常地方便,再也不用去依赖第三方的包
// 同步请求 HttpClient client = HttpClient.newBuilder() .version(Version.HTTP_1_1) .followRedirects(Redirect.NORMAL) .connectTimeout(Duration.ofSeconds(20)) .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) .authenticator(Authenticator.getDefault()) .build(); HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); System.out.println(response.statusCode()); System.out.println(response.body()); // 异步请求 HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://foo.com/")) .timeout(Duration.ofMinutes(2)) .header("Content-Type", "application/json") .POST(BodyPublishers.ofFile(Paths.get("file.json"))) .build(); client.sendAsync(request, BodyHandlers.ofString()) .thenApply(HttpResponse::body) .thenAccept(System.out::println);
7 ZGC
官方宣称ZGC的垃圾回收停顿时间不超过10ms,能支持高达16TB的堆空间,并且停顿时间不会随着堆的增大而增加。
浙公网安备 33010602011771号