Changes from Java 8 to Java 11

Java 8 to Java 11

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

language

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.langandjava.util --It is better not to usevar 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 ʻIntegerandLongmethods, 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 withvar`, 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");
}};

_ Not available

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

function

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

Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
The road from JavaScript to Java
[Java] Conversion from array to List
Changes in Java 11
[Java] Introduction to Java
Introduction to java
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
Call Java from JRuby
[Java] From two Lists to one array list
Upsert from Java SDK to Azure Cosmos DB
Run R from Java I want to run rJava
Migrate from JUnit 4 to JUnit 5
Connect to Aurora (MySQL) from a Java application
To become a VB.net programmer from a Java shop
Eval Java source from Java
[Java] Connect to MySQL
Migrate from Java to Server Side Kotlin + Spring-boot
How to get Class from Element in Java
Access API.AI from Java
Kotlin's improvements to Java
[Java] How to switch from open jdk to oracle jdk
I want to write quickly from java to sqlite
Introduction to java command
Minecraft BE server development from PHP to Java
Select * from Java SDK to Azure Cosmos DB
Launch Docker from Java to convert Office documents to PDF
Convert Java enum enums and JSON to and from Jackson
[Java] I want to calculate the difference from the date
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
[Java] How to extract the file name from the path
6 features I missed after returning to Java from Scala
Java development for beginners to start from 1-Vol.1-eclipse setup
How to change from Oracle Java 8 to Adopt Open JDK 9
[Java] How to erase a specific character from a character string
Generate models from JSON to Swift, PHP, C #, JAVA
[Java] Platforms to choose from for Java development starting now (2020)
Moved from iBATIS to MyBatis3
[Java] How to use Map
Try Spring Boot from 0 to 100.
[Java] How to use Map