Whats New in Java 8

591 views
462 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
591
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Whats New in Java 8

  1. 1. What’s new in Java 8 Maxim Zakharenkov 3 April, 2014
  2. 2. 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
  3. 3. What this talk is about • Streams • Stream notes • Some other new features in Java 8 – Optional<?> – Annotations – parallelSort – Concurrency – Some other stuff
  4. 4. Streams Other presentation slides here
  5. 5. filter()
  6. 6. filter() map()
  7. 7. filter() map() map()
  8. 8. filter() map() map() distinct()
  9. 9. filter() map() map() distinct()collect()
  10. 10. Stream notes: Short circuiting Random random = new Random(); int[] result = IntStream .generate(() ->random.nextInt()) .sorted() .toArray();
  11. 11. Stream notes: parallel forEach DEMO
  12. 12. Stream notes: parallel forEach • forEach does not preserve order! • Use forEachOrdered if order matters
  13. 13. 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();
  14. 14. Optional results 1 public String findById(int id) { // ... return …; }
  15. 15. Optional results 2 public String findById(int id) { // ... return …; } String result = findById(10);
  16. 16. Optional results 3 public String findById(int id) { // ... return …; } String result = findById(10); prefix = result.substring(0, 3);
  17. 17. Optional results 4 public String findById(int id) { // ... return …; } String result = findById(10); if(result != null) { prefix = result.substring(0, 3); }
  18. 18. Optional results 5 public String ? findById(int id) { // ... return …; } String result = findById(10); prefix = result.substring(0, 3);
  19. 19. Optional results 6 public Optional<String> findById(int id) { // ... return …; } if(result.isPresent()) { }
  20. 20. Annotations public @NonNull String findById(int id) { // ... return …; }
  21. 21. Checker framework http://types.cs.washington.edu/checker- framework/current/checkers-manual.html
  22. 22. 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
  23. 23. Arrays.parallelSort • Works faster • Everyting has own cost – DualPivotQuickSort - sequental – MergeSort - parallel
  24. 24. Parallel sort of 50M elements (on my 4 core laptop) • Single threaded DualPivotQuickSort: 47sec • Parallel merge sort: 21sec • Sequential stream: 95sec • Parallel stream: 104sec
  25. 25. 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
  26. 26. HashMap 0 key1 1 2 3 4 5 6 key2 key3
  27. 27. HashMap before Java 8 0 key1 1 2 3 4 5 6 key2 key3 key4 key4
  28. 28. HashMap in Java 8 0 key1 1 2 3 4 5 6 key2 key3 key4 key4
  29. 29. MissionControl • Cool tool for JVM diagnostics • Comes with JDK installation (jmc executable • Now we have 3 similar tools in JDK – jconsole – jvisualvm – jms
  30. 30. 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 • - ….
  31. 31. QA

×