http://openjdk.java.net/projects/jdk9/ http://cr.openjdk.java.net/~iris/se/9/latestSpec/apidiffs/overview-summary.html
https://openjdk.java.net/projects/jdk/10/ http://cr.openjdk.java.net/~iris/se/10/latestSpec/apidiffs/overview-summary.html
https://openjdk.java.net/projects/jdk/11/ http://cr.openjdk.java.net/~iris/se/11/latestSpec/apidiffs/overview-summary.html
First, there are changes about the language.
var http://openjdk.java.net/jeps/286 You can now type infer about local variables.
var a = "Foo";
I think the following is a good guideline.
--For those with types on the right side, such as new
and List.of
, it is better to use var
(other than ʻInteger,
Long, etc.) --You can use
var for basic classes such as
java.langand
java.util --It is better not to use
var because wrapper classes such as ʻInteger
and Long
are handled with caution.
--You can use var
for the basic type
--Don't use var
for application classes
When using a variable of the wrapper class, it handles null, so I think it is better to specify the type. If you don't use null, use the basic type. When applied to item 1, the following writing style is OK.
var count = Long.valueOf("123");
However, for ʻIntegerand
Longmethods, there are some that return the basic type and some that return the wrapper type, so it is safer to specify the type. If you receive it with
var`, it is better to make it a basic type.
private interface methods http://openjdk.java.net/jeps/213
You can now use private methods for interface.
Since the static method and default method can be used in Java8, the common processing can be written in private
.
effective final in try-with-resource http://openjdk.java.net/jeps/213
For variables that are practically final, you can now specify only variables with try-with-resource.
Until now, if you wanted to automatically close a Closable
variable defined outside try
with try-with-resource, you had to assign it to another variable. For example, if you want to use it finally, you need to define a variable outside of try, but you can use it in such a case.
var io = new InputStream();
try (var io2 = io) {
...
}
↓
var io = new InputStream();
try (io) {
...
}
<> in anonymous You can now type infer anonymous classes with the diamond operator.
List<String> strs = new ArrayList<>() {{
this.add("aa");
this.add("bb");
}};
You can no longer use _
as a variable.
API
From here, we will summarize the changes in the standard library for each class.
Click here for Java 11 API https://nowokay.hatenablog.com/entry/20180704/1530712754
java.lang
String Compact String http://openjdk.java.net/jeps/254 It is included in Java 9. Until Java8, characters were held as char in UTF-16, so 2 bytes were used for each character, but character strings containing only ASCII characters such as alphabets and numbers are now held in 1 byte.
Indy String concatenation http://openjdk.java.net/jeps/280
Until now, string concatenation with +
was expanded to StringBuilder
, but it will be confirmed at runtime with Invoke Dynamic.
"aa"+ a
↓
new StringBuilder("aa").append(a);
↓ Dynamic Invoke
repeat(n) Can be repeated
jshell> "test".repeat(3)
$1 ==> "testtesttest"
isBlank()
isBlank is a UTF space character
strip() / stripLeading() / stripTrailing()
UTF version of trim () / trimLeft () / trimRight ()
lines()
Returns a Stream of strings for each newline
"test\nprogram\n".lines()
.map(String::strip)
.collect(Collectors.joining("\n")
You can remove the space before and after each line by doing like.
java.util
List/Set/Map of http://openjdk.java.net/jeps/269
Returns an Immutable collection
var strs = List.of("apple", "banana");
var sets = Set.of("apple", "banana");
var dict = Map.of(
"apple", "Apple",
"banana", "banana");
copyOf Returns an Immutable copy
Map ofEntries() / entry()
Not only ʻof but also ʻofEntries ()
has been added to the Map.
var map = Map.ofEntries(
entry("key1", "value1"),
entry("key2", "value2"));
Predicate not
Convenient inversion of method reference
lines.stream()
.filter(not(String::isEmpty))
.forEach(System.out::println);
Collection toArray(IntFunction)
When converting List etc. to an array, it was necessary to prepare an array with the same size in advance, but you can generate an array like strs.toArray (String [] :: new)
Stream dropWhile / takeWhile
In Java 8, Stream processing such as "from start to end in data" was not possible, but it can be implemented by using dropWhile / takeWhile introduced in Java 9.
jshell> Stream.of("one", "start", "two", "three", "end", "four").
...> dropWhile(s -> !s.equals("start")).
...> takeWhile(s -> !s.equals("end")).
...> forEach(System.out::println)
start
two
three
In this case, it contains up to start
, so you need to skip it.
jshell> Stream.of("one", "start", "two", "three", "end", "four").
...> dropWhile(s -> !s.equals("start")).
...> skip(1).
...> takeWhile(s -> !s.equals("end")).
...> forEach(System.out::println)
two
three
ofNullable
iterate
Iterate with two arguments has been available since Java8, but it was necessary to set a stop condition with limit etc.
IntStream.iterate(0, i -> i + 3).limit(5)
.forEach(System.out::println);
I didn't have takeWhile
either, so it was quite difficult to use.
And now you can use ʻiterate in combination with
takeWhile`.
So you can use it like a for statement.
IntStream.iterate(0, i -> i < 10, i -> i + 3)
.forEach(System.out::println);
If you write this in the following way, it will not come back. Think about why.
IntStream.iterate(0, i -> i < 10, i -> i++)
.forEach(System.out::println);
I can do this
IntStream.iterate(0, i -> i < 10, i -> ++i)
.forEach(System.out::println);
Optional/OptionalInt/OptionalLong/OptionalDouble orElseThrow() isEmpty() stream()
Converting from Optional to Stream has never been easier.
This eliminates the need to insert a filter (Optional :: isPresent)
when an Optional returns in a Stream.
For example, if getById
returns Optional
ids.map(Data::getById)
.filter(Optional::isPresent)
.collect(Collectors.toList())
What I was doing like
ids.flatMap(id -> getById(id).stream())
.collect(Collectors.toList())
You will be able to write with flatMap like this.
ifPresentOrElse or
If Optional is Empty, it is easier to write something like the following Optional.
Collectors toUnmodifiableList / toUnmodifiableSet / toUnmodifiableMap
A Collector that stores values in an immutable collection
filtering flatMapping
Enumeration asIterator
Objects requireNonNullElse requireNonNullElseGet
java.io
Path of
Files writeString
The process of writing text to a file is in one line.
readString
The process of reading text from a file is one line.
Reader transferTo
Writing the content read by Reader as it is in Writer is one line
nullReader Writer nullWriter
InputStream transferTo
Writing the content read by InputStream as it is to OutputStream is one line
nullInputStream
OutputStream nullOutputStream
java.net.http
HTTP access has been revamped.
JVM
Log http://openjdk.java.net/jeps/158 http://openjdk.java.net/jeps/271
G1GC is the default GC http://openjdk.java.net/jeps/248
ZGC
CMS is deprecated http://openjdk.java.net/jeps/291
Ahead of time compilation http://openjdk.java.net/jeps/295
JShell http://openjdk.java.net/jeps/222
Launch Single-File Source-Code Programs
You can now run a single source Java code with java commands without compiling it with javac.
Module System
Java EE and CORBA is dropped
Recommended Posts