What’s new in Java 8
Maxim Zakharenkov
3 April, 2014
Existing presentations
• Новое в JDK 8. Александр Ильин, Oracle
https://www.youtube.com/watch?v=lSnNWRABA1s
• JDK8: Stream style, Sergey Kuksenko, Oracle
http://www.slideshare.net/SergeyKuksenko/jdk8-stream-style
• More on slideshare
What this talk is about
• Streams
• Stream notes
• Some other new features in Java 8
– Optional<?>
– Annotations
– parallelSort
– Concurrency
– Some other stuff
Streams
Other presentation slides here
filter()
filter()
map()
filter()
map()
map()
filter()
map()
map()
distinct()
filter()
map()
map()
distinct()collect()
Stream notes: Short circuiting
Random random = new Random();
int[] result = IntStream
.generate(() ->random.nextInt())
.sorted()
.toArray();
Stream notes: parallel forEach
DEMO
Stream notes: parallel forEach
• forEach does not preserve order!
• Use forEachOrdered if order matters
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();
Optional results 1
public String findById(int id) {
// ...
return …;
}
Optional results 2
public String findById(int id) {
// ...
return …;
}
String result = findById(10);
Optional results 3
public String findById(int id) {
// ...
return …;
}
String result = findById(10);
prefix = result.substring(0, 3);
Optional results 4
public String findById(int id) {
// ...
return …;
}
String result = findById(10);
if(result != null) {
prefix = result.substring(0, 3);
}
Optional results 5
public String ? findById(int id) {
// ...
return …;
}
String result = findById(10);
prefix = result.substring(0, 3);
Optional results 6
public Optional<String>
findById(int id) {
// ...
return …;
}
if(result.isPresent()) {
}
Annotations
public @NonNull String findById(int id) {
// ...
return …;
}
Checker framework
http://types.cs.washington.edu/checker-
framework/current/checkers-manual.html
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
Arrays.parallelSort
• Works faster
• Everyting has own cost
– DualPivotQuickSort - sequental
– MergeSort - parallel
Parallel sort of 50M elements
(on my 4 core laptop)
• Single threaded DualPivotQuickSort: 47sec
• Parallel merge sort: 21sec
• Sequential stream: 95sec
• Parallel stream: 104sec
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
HashMap
0 key1
1
2
3
4
5
6
key2
key3
HashMap before Java 8
0 key1
1
2
3
4
5
6
key2
key3
key4 key4
HashMap in Java 8
0 key1
1
2
3
4
5
6
key2
key3
key4
key4
MissionControl
• Cool tool for JVM diagnostics
• Comes with JDK installation (jmc executable
• Now we have 3 similar tools in JDK
– jconsole
– jvisualvm
– jms
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
• - ….
QA

Whats New in Java 8