前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java10的新特性

Java10的新特性

作者头像
code4it
发布2018-09-17 16:08:18
4560
发布2018-09-17 16:08:18
举报
文章被收录于专栏:码匠的流水账码匠的流水账

Java语言特性系列

  • Java5的新特性
  • Java6的新特性
  • Java7的新特性
  • Java8的新特性
  • Java9的新特性
  • Java10的新特性
  • Java11的新特性

本文主要讲述一下Java10的新特性

特性列表

  • 286: Local-Variable Type Inference(重磅) 相关解读: java10系列(二)Local-Variable Type Inference
  • 296: Consolidate the JDK Forest into a Single Repository
  • 304: Garbage-Collector Interface
  • 307: Parallel Full GC for G1
  • 310: Application Class-Data Sharing
  • 312: Thread-Local Handshakes
  • 313: Remove the Native-Header Generation Tool (javah)
  • 314: Additional Unicode Language-Tag Extensions
  • 316: Heap Allocation on Alternative Memory Devices
  • 317: Experimental Java-Based JIT Compiler(重磅) 相关解读: Java10来了,来看看它一同发布的全新JIT编译器
  • 319: Root Certificates 相关解读: OpenJDK 10 Now Includes Root CA Certificates
  • 322: Time-Based Release Versioning 相关解读: java10系列(一)Time-Based Release Versioning

细项解读

上面列出的是大方面的特性,除此之外还有一些api的更新及废弃,主要见What’s New in JDK 10 - New Features and Enhancements,这里举几个例子。

Optional.orElseThrow()

/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/lib/src.zip!/java.base/java/util/Optional.java

  • 源码 /** * If a value is present, returns the value, otherwise throws * {@code NoSuchElementException}. * * @return the non-{@code null} value described by this {@code Optional} * @throws NoSuchElementException if no value is present * @since 10 */ public T orElseThrow() { if (value == null) { throw new NoSuchElementException("No value present"); } return value; }
  • 实例 @Test public void testOrElseThrow(){ var data = List.of("a","b","c"); Optional<String> optional = data.stream() .filter(s -> s.startsWith("z")) .findAny(); String res = optional.orElseThrow(); System.out.println(res); } 新增了orElseThrow与get相对应

输出

代码语言:javascript
复制
java.util.NoSuchElementException: No value present

    at java.base/java.util.Optional.orElseThrow(Optional.java:371)
    at com.example.FeatureTest.testOrElseThrow(FeatureTest.java:19)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

APIs for Creating Unmodifiable Collections

java9新增的of工厂方法的接口参数是一个个元素,java10新增List.copyOf, Set.copyOf,及Map.copyOf用来从已有集合创建ImmutableCollections

  • List.copyOf源码 /** * Returns an <a href="#unmodifiable">unmodifiable List</a> containing the elements of * the given Collection, in its iteration order. The given Collection must not be null, * and it must not contain any null elements. If the given Collection is subsequently * modified, the returned List will not reflect such modifications. * * @implNote * If the given Collection is an <a href="#unmodifiable">unmodifiable List</a>, * calling copyOf will generally not create a copy. * * @param <E> the {@code List}'s element type * @param coll a {@code Collection} from which elements are drawn, must be non-null * @return a {@code List} containing the elements of the given {@code Collection} * @throws NullPointerException if coll is null, or if it contains any nulls * @since 10 */ @SuppressWarnings("unchecked") static <E> List<E> copyOf(Collection<? extends E> coll) { if (coll instanceof ImmutableCollections.AbstractImmutableList) { return (List<E>)coll; } else { return (List<E>)List.of(coll.toArray()); } }
  • 实例 @Test(expected = UnsupportedOperationException.class) public void testCollectionCopyOf(){ List<String> list = IntStream.rangeClosed(1,10) .mapToObj(i -> "num"+i) .collect(Collectors.toList()); List<String> newList = List.copyOf(list); newList.add("not allowed"); }

Collectors新增了toUnmodifiableList, toUnmodifiableSet,以及 toUnmodifiableMap方法

代码语言:javascript
复制
    @Test(expected = UnsupportedOperationException.class)
    public void testCollectionCopyOf(){
        List<String> list = IntStream.rangeClosed(1,10)
                .mapToObj(i -> "num"+i)
                .collect(Collectors.toUnmodifiableList());
        list.add("not allowed");
    }

小结

java10最主要的新特性,在语法层面就属于Local-Variable Type Inference,而在jvm方面307: Parallel Full GC for G1以及317: Experimental Java-Based JIT Compiler都比较重磅,值得深入研究。

doc

  • JDK 10 Features
  • Introducing Java SE 10(官方解读)
  • What’s New in JDK 10 - New Features and Enhancements(官方细项解读)
  • JDK-8140281 : (opt) add no-arg orElseThrow() as preferred alternative to get()
  • JDK-8177290 : add copy factory methods for unmodifiable List, Set, Map
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-03-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 特性列表
  • 细项解读
    • Optional.orElseThrow()
      • APIs for Creating Unmodifiable Collections
      • 小结
      • doc
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档