Java language features series
- New features in Java5
- New features for Java6
- New features in Java7
- New features in Java8
- New features in Java9
- New features in Java10
- New features for Java11
sequence
This article focuses on the new features of Java10
Feature list
- 286: Local-Variable Type Inference(
blockbuster
)
(2)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(
blockbuster
)
Related: Java10 is here, and check out the new JIT compiler it shipped with
- 319: Root Certificates
OpenJDK 10 Now Includes Root CA Certificates
- 322: Time-Based Release Versioning
Java10 series (1) Time-based Release Versioning
Details of interpretation
Listed above are the major Features, but there are also some API updates and Enhancements (see What’s New in JDK 10 – New Features and Enhancements, for example).
Optional.orElseThrow()
/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/lib/src.zip! /java.base/java/util/Optional.java
- The source code
/**
* 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;
}
Copy the code
- The instance
@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);
}
Copy the code
Added orElseThrow to correspond to get
The output
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.FrameworkMethodThe $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.ParentRunnerThe $1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.accessThe $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)
Copy the code
APIs for Creating Unmodifiable Collections
Java9 added a new of factory method whose interface parameters are individual elements, and Java10 added list. copyOf, set. copyOf, and map. copyOf to create ImmutableCollections from existing collections
- List. The copyOf the source code
/**
* 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
List
copyOf(Collection
coll) { if (coll instanceof ImmutableCollections.AbstractImmutableList) { return (List
)coll; } else { return (List
)List.of(coll.toArray()); }}
Copy the code
- The instance
@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");
}
Copy the code
The toUnmodifiableList, toUnmodifiableSet, and toUnmodifiableMap methods are added
@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");
}
Copy the code
summary
The Local Variable Type Inference (307: Parallel Full GC for G1) and the 317: Parallel Full GC for G1 Experimental Java-based JIT Compiler is relatively important and worthy of further study.
doc
- JDK 10 Features
- Introducing Java SE 10(
The official interpretation
) - What’s New in JDK 10 – New Features and Enhancements(
Official detail interpretation
) - JDK-8140281 : (opt) add no-arg orElseThrow() as preferred alternative to get()
- JDK-8177290 : add copy factory methods for unmodifiable List, Set, Map