Java 10 major language and API improvements
Java ⑩ has been released in March 2018.
The following content is summarizing the most important changes to the Java API and the Java language introduced with Java ⑩.
For a more complete overview follow the links in the following sections referring to the official Oracle release documents.
Java 10 Language improvements
The following describes selected improvements of the Java language. See the Java 10 release notes.
Local Variable Type Inference
var
is not a keyword but a reserved type identifier.
The type is inferred from the initializer.
See https://openjdk.java.net/projects/amber/LVTIstyle.html
which discusses when var
should be used and when best avoided.
var source = List.of("one", "two");
var copy = List.copyOf(source);
Java 10 API improvements
The following describes selected improvements of the Java API. See the Java 10 release notes.
Collectors
List<String> copy=source
.stream()
.collect(Collectors.toUnmodifiableList());
With Collectors.toUnmodifiableList()
we can now convert a list into an immutable list.
Set<String> copy=source
.stream()
.collect(Collectors.toUnmodifiableSet());
With Collectors.toUnmodifiableSet()
we can now convert a set into an immutable set.
Map<Integer,String> copy=source
.entrySet()
.stream()
.collect(Collectors
.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
With Collectors.toUnmodifiableMap()
we can now convert a map into an immutable map.
Map
var source = Map
.of(1, "one", 2, "two");
var copy = Map.copyOf(source);
With Map.copyOf
we can now create an immutable copy of a list.
Optional
Optional.of("OK")
.orElseThrow();
A argumentless method Optional.orElseThrow()
that throws a NoSuchElementException
has been added.
Set
var source = Set.of("one", "two");
var copy = Set.copyOf(source);
With Set.copyOf
we can now create an immutable copy of a list.
List
var source = List.of("one", "two");
var copy = List.copyOf(source);
With List.copyOf
we can now create an immutable copy of a list.
JVM Runtime
Container Awareness
A JVM running in a container is now aware of this and CPU and memory information is now reported as allocated by the container.