Java 9 - The JDK

by Vlad Gaevsky

About Me

  • Java developer
  • Core member of JProf.by
  • Keen on new technologies

Big fan of: Kotlin, Spring Boot, Gradle, Lombok, etc.

@kelstar95

vlad.gaevsky@gmail.com

t.me/kelstar

Collections

List<String> list = List.of("jprof", "by", "meetup");

Set<String> set = Set.of("jprof", "by", "meetup");

Java 8 Way

Java 9 Way

List<String> list = Arrays.asList("jprof", "by", "meetup");
list = Collections.unmodifiableList(list);

Set<String> set = new HashSet<>(Arrays.asList("jprof", "by", "meetup"));
set = Collections.unmodifiableSet(set);

Collections

Map map = Map.of("java", 1, "by", 2, "meetup", 3);

Java 8 Way

Java 9 Way

Map<String, Integer> map = new HashMap<String, Integer>(); 
map.put("jprof", 1); 
map.put("by", 2);
map.put("meetup", 3);
map = Collections.unmodifiableMap(map);
Map <Integer, String> map = Map.ofEntries(
   entry(1, "First"),
   entry(2, "Second"),
   entry(3, "Third")
);

Collections

Will it compile?

List<String> list = List.of("jprof", "by", "meetup");
list.add("java 9");

Well, yes... but...

Collections

jshell> List.of(1).getClass()
$1 ==> class java.util.ImmutableCollections$List1

jshell> List.of(1, 2).getClass()
$2 ==> class java.util.ImmutableCollections$List2

jshell> List.of(1, 2, 3).getClass()
$3 ==> class java.util.ImmutableCollections$ListN
jshell> Set.of(1,2,3)
$1 ==> [2, 3, 1]

jshell> Set.of(1,2,3)
$2 ==> [2, 3, 1]

jshell> /reset
|  Resetting state.

jshell> Set.of(1,2,3)
$1 ==> [3, 2, 1]

Optimizations

Randomness

Streams

  • dropWhile
  • takeWhile
  • iterate
  • ofNullable

Streams

Stream.of(1,2,3,4,5,6,7,8,9,10)
    .takeWhile(x -> x < 4)
    .forEach(System.out::println);

1
2
3
Stream.of(1,2,3,4,5,6,7,8,9,10)
    .dropWhile(x -> x < 4)
    .forEach(System.out::println);

4
5
6
7
8
9
10

takeWhile

dropWhile

Streams

IntStream.iterate(0, x -> x < 3, x -> x + 1)
    .forEach(System.out::println);
0
1
2
Stream.ofNullable(1)
    .forEach(System.out::println);
-> 1

Stream.ofNullable(null)
    .forEach(System.out::println);
->

iterate

ofNullable

Optionals

as Stream

Optional.empty().stream()
jshell> Optional.of(1).map(x -> x * 3)
$1 ==> Optional[3]
 
jshell> Optional.of(1).stream().map(x -> x * 3)
$2 ==> java.util.stream.ReferencePipeline$3@67b92f

Laziness

Optionals

or

jshell> Optional.empty().or(() -> Optional.of("Java 9"))
$1 ==> Optional[Java 9]
Optional.empty()
    .ifPresentOrElse(
        System.out::println, 
        () -> System.out.println("oops")
    );

ifPresentOrElse

Completable Future

JEP 266: More Concurrency Updates

Completable Future

CompletableFuture<T> completeAsync(Supplier<T> supplier, Executor executor)
Executor defaultExecutor()
CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)
CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)

Completable Future

jshell> CompletableFuture<String> future = new CompletableFuture<>()
future ==> java.util.concurrent.CompletableFuture@35d176f7[Not completed]
jshell> future.copy()
$1 ==> java.util.concurrent.CompletableFuture@4973813a[Not completed]
jshell> future.isDone()
$2 ==> false
jshell> $1.isDone()
$3 ==> false
jshell> $1.complete("JProf")
$4 ==> true
jshell> $1.isDone()
$5 ==> true
jshell> future.isDone()
$6 ==> false

Completable Future

jshell> CompletableFuture<String> future = new CompletableFuture<>()
future ==> java.util.concurrent.CompletableFuture@4bbfb90a[Not completed]
jshell> future.copy()
$1 ==> java.util.concurrent.CompletableFuture@5a8806ef[Not completed]
jshell> future.complete("jprof")
$2 ==> true
jshell> $1.isDone()
$3 ==> true

Completable Future

Executor delayedExecutor(long delay, TimeUnit unit, Executor executor)
CompletableFuture<U> failedFuture(Throwable ex)

Reactive

4 Java interfaces:

  • Processor
  • Publisher
  • Subscriber
  • Subscription
java.util.concurrent.Flow

declared at

Stack SkyWalker

StackWalker class

  • getInstance
  • forEach
  • walk

Stack SkyWalker

List<StackFrame> frames = StackWalker.getInstance().walk(s ->
     s.takeWhile(f -> f.getClassName().startsWith("by.jprof."))
      .limit(10)
      .collect(Collectors.toList()));

Example

Why?

HTTP/2

  • First incubator module
  • Replace HttpURLConnection
  • Full support of HTTP 2.0 and WebSockets

3 classes to run the show

  • HttpClient
  • HttpRequest
  • HttpResponse

HTTP/2

HttpClient httpClient = HttpClient.newHttpClient(); //Create a HttpClient

HttpRequest httpRequest = HttpRequest.newBuilder()
    .uri(new URI("https://www.google.com/"))
    .GET()
    .build(); //Create a GET request for the given URI

HttpResponse<String> httpResponse = 
    httpClient.send(httpRequest, HttpResponse.BodyHandler.asString());

Example

Async way!

CompletableFuture<HttpResponse<String>> futureResponse =
    httpClient.sendAsync(httpRequest, HttpResponse.BodyHandler.asString());

Links

Presentation

Recap

  • Collections, Streams, Optionals
  • Concurrency (Completable Future, Reactive)
  • Stack-Walking API
  • HTTP2 incubator module

Oracle Java 9 Doc

Questions

Java 9: The JDK

By Vlad Gaevsky

Java 9: The JDK

  • 1,659