by Vlad Gaevsky
Big fan of: Kotlin, Spring Boot, Gradle, Lombok, etc.
@kelstar95
vlad.gaevsky@gmail.com
t.me/kelstar
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);
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")
);
Will it compile?
List<String> list = List.of("jprof", "by", "meetup");
list.add("java 9");
Well, yes... but...
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
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
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
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
or
jshell> Optional.empty().or(() -> Optional.of("Java 9"))
$1 ==> Optional[Java 9]
Optional.empty()
.ifPresentOrElse(
System.out::println,
() -> System.out.println("oops")
);
ifPresentOrElse
JEP 266: More Concurrency Updates
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)
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
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
Executor delayedExecutor(long delay, TimeUnit unit, Executor executor)
CompletableFuture<U> failedFuture(Throwable ex)
4 Java interfaces:
java.util.concurrent.Flow
declared at
StackWalker class
List<StackFrame> frames = StackWalker.getInstance().walk(s ->
s.takeWhile(f -> f.getClassName().startsWith("by.jprof."))
.limit(10)
.collect(Collectors.toList()));
Example
Why?
3 classes to run the show
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());
Presentation
Oracle Java 9 Doc