Whats New in Java 8

39
What’s new in Java 8 Maxim Zakharenkov 3 April, 2014

Transcript of Whats New in Java 8

Page 1: Whats New in Java 8

What’s new in Java 8

Maxim Zakharenkov3 April, 2014

Page 2: Whats New in Java 8

Existing presentations

• Новое в JDK 8. Александр Ильин, Oraclehttps://www.youtube.com/watch?v=lSnNWRABA1s

• JDK8: Stream style, Sergey Kuksenko, Oraclehttp://www.slideshare.net/SergeyKuksenko/jdk8-stream-style

• More on slideshare

Page 3: Whats New in Java 8

What this talk is about

• Streams• Stream notes• Some other new features in Java 8– Optional<?>– Annotations– parallelSort– Concurrency– Some other stuff

Page 4: Whats New in Java 8

Streams

Other presentation slides here

Page 5: Whats New in Java 8
Page 6: Whats New in Java 8
Page 7: Whats New in Java 8
Page 8: Whats New in Java 8
Page 9: Whats New in Java 8
Page 10: Whats New in Java 8
Page 11: Whats New in Java 8
Page 12: Whats New in Java 8
Page 13: Whats New in Java 8

filter()

Page 14: Whats New in Java 8

filter()

map()

Page 15: Whats New in Java 8

filter()

map()map()

Page 16: Whats New in Java 8

filter()

map()map()

distinct()

Page 17: Whats New in Java 8

filter()

map()map()

distinct()collect()

Page 18: Whats New in Java 8

Stream notes: Short circuiting

Random random = new Random();int[] result = IntStream

.generate(() ->random.nextInt())

.sorted()

.toArray();

Page 19: Whats New in Java 8

Stream notes: parallel forEach

DEMO

Page 20: Whats New in Java 8

Stream notes: parallel forEach

• forEach does not preserve order!• Use forEachOrdered if order matters

Page 21: Whats New in Java 8

Stream notes: not reusable

IntStream stream = Arrays.stream(new int[]{1, 2, 3, 4}); List<Object> list = stream.boxed().collect(Collectors.toList()); // this throws java.lang.IllegalStateException double average = stream.average().getAsDouble();

Page 22: Whats New in Java 8

Optional results 1

public String findById(int id) { // ... return …;}

Page 23: Whats New in Java 8

Optional results 2

public String findById(int id) { // ... return …;}

String result = findById(10);

Page 24: Whats New in Java 8

Optional results 3

public String findById(int id) { // ... return …;}

String result = findById(10);

prefix = result.substring(0, 3);

Page 25: Whats New in Java 8

Optional results 4

public String findById(int id) { // ... return …;}

String result = findById(10);

if(result != null) {prefix = result.substring(0, 3);

}

Page 26: Whats New in Java 8

Optional results 5

public String ? findById(int id) { // ... return …;}

String result = findById(10);

prefix = result.substring(0, 3);

Page 27: Whats New in Java 8

Optional results 6

public Optional<String> findById(int id) { // ... return …;}

if(result.isPresent()) {}

Page 28: Whats New in Java 8

Annotations

public @NonNull String findById(int id) { // ... return …;}

Page 29: Whats New in Java 8

Checker framework

http://types.cs.washington.edu/checker-framework/current/checkers-manual.html

Page 30: Whats New in Java 8

Checker framework

• @Interned String intern() { ... } // return value • int compareTo(@NonNull String other) { ... } // parameter • String toString(@ReadOnly MyClass this) { ... } // receiver• @NonNull List<@Interned String> messages; // generics: non-

null list of interned Strings • @Interned String @NonNull [] messages; // arrays: non-null

array of interned Strings • myDate = (@ReadOnly Date) readonlyObject; // cast

Page 31: Whats New in Java 8

Arrays.parallelSort

• Works faster• Everyting has own cost– DualPivotQuickSort - sequental– MergeSort - parallel

Page 32: Whats New in Java 8

Parallel sort of 50M elements(on my 4 core laptop)

• Single threaded DualPivotQuickSort: 47sec• Parallel merge sort: 21sec• Sequential stream: 95sec• Parallel stream: 104sec

Page 33: Whats New in Java 8

Parallelism, is it good or bad?(example)

• 2 CPU• Concurrent clients - 1000 queries per second• Single sequential sort - 2ms• Single parallel sort - 1ms + sync overhead ~1.2ms• Less transactions per second with parallel sort• Parallel algorithms do not always fit well

Page 34: Whats New in Java 8

HashMap

0 key1

1

2

3

4

5

6

key2

key3

Page 35: Whats New in Java 8

HashMap before Java 8

0 key1

1

2

3

4

5

6

key2

key3

key4 key4

Page 36: Whats New in Java 8

HashMap in Java 8

0 key1

1

2

3

4

5

6

key2

key3

key4

key4

Page 37: Whats New in Java 8

MissionControl

• Cool tool for JVM diagnostics• Comes with JDK installation (jmc executable• Now we have 3 similar tools in JDK– jconsole– jvisualvm– jms

Page 38: Whats New in Java 8

Future of frameworks

• Type-safe property references in Hibernate-like query languages

• JUnit, Mockito-like frameworks with method references

• Event handlers• Better XML and JSON serializers• - ….

Page 39: Whats New in Java 8

QA