Your SlideShare is downloading. ×

Jug Marche: Meeting June 2014. Java 8 hands on

370

Published on

Hands-on Java 8 with examples and open discussion about the more relevant new feature of Java 8: lambdas, streams, CompletableFeature, new Date & Time API.

Hands-on Java 8 with examples and open discussion about the more relevant new feature of Java 8: lambdas, streams, CompletableFeature, new Date & Time API.

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

  • Be the first to like this

No Downloads
Views
Total Views
370
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java 8 What’s new? Onofrio Panzarino @onof80 JUG Marche - June 2014
  • 2. Agenda 1. Lambda expressions 2. Stream API 3. Parallel operations 4. Default methods 5. Date time API 6. Optional: null is evil! 7. CompletableFuture
  • 3. Lambda expressions (lambda (x) (+ x 1)) LISP function (x) { return x + 1; }; JavaScript [](auto x) { return x + 1; }; C++11 (int x) => x + 1; C# (_:Int) + 1 Scala lambda x: x + 1 Python x -> x + 1 Haskell
  • 4. Java’s Lambdas Syntax: (arguments) -> { body } Collections.sort(employees, (Employee p1, Employee p2) -> p1.getSurname().compareTo(p2.getSurname())) new Thread(() -> { System.out.println("Thread started!"); }).start();
  • 5. Stream API On collections: all.stream() .filter(e -> e.getRole().equalsIgnoreCase(role)) .sorted(Comparator.comparing(e -> e.getBirthDate())) .map(e -> e.getFullName()) .collect(Collectors.joining(separator)); On files: Files.lines(Paths.get("myFile.txt")) .map(String::trim) .filter(s -> ! s.isEmpty()). // etc
  • 6. Parallel Streams Very difficult: all.parallelStream() .filter(e -> e.getRole().equalsIgnoreCase(role)) .sorted(Comparator.comparing(e -> e.getBirthDate())) .map(e -> e.getFullName()) .forEach(System.out::println);
  • 7. Default methods public interface Person { String getName(); String getSurname(); default String getFullName() { return getSurname() + " " + getName(); } } Multiple inheritance ? -> Must implement the method explicitly
  • 8. New Dates API ● LocalDate, LocalTime and LocalDateTime ● ZonedDateTime ● Period vs Duration ● Chronologies http://www.oracle.com/technetwork/articles/java /jf14-date-time-2125367.html
  • 9. Optional .isPresent(); // (boolean) .get(); .orElse(defaultValue); .orElseGet( () -> new Employee(...) ); .orElseThrow( () -> new IllegalArgumentException() ); Functional: .flatMap(e -> Optional.of(e.getAge()) ) ... .filter(e -> e.getAge().getYears() > 50 ) ... .map(e -> e.getAge() ) ... .ifPresent(System.out::println); .flatMap( e -> e.getItems().findFirst() );
  • 10. CompletableFuture CompletableFuture<String> future = CompletableFuture .supplyAsync(() -> "Hello future!"); future.handle((msg, ex) -> { if (ex != null) { return "Houston! " + ex.getMessage(); } else { return msg; } }) .thenAcceptAsync(a -> System.out.println(a));
  • 11. CompletableFuture Future composition! CompletableFuture<Stream<String>> future1 = CompletableFuture .supplyAsync(() -> repo1.getAll()); CompletableFuture<Stream<String>> future2 = CompletableFuture .supplyAsync(() -> repo2.verySlowQuery()); future1 .thenCombineAsync(future2, Stream::concat) .thenAccept(s -> { System.out.print("Found: "); s.forEach(System.out::println); }) .join()
  • 12. References http://www.oracle.com/technetwork/java/javase/ 8-whats-new-2157071.html http://www.informit.com/articles/article.aspx?p= 2191426 Books: http://www.manning.com/urma/
  • 13. Open Discussion Thank you! Onofrio Panzarino @onof80 JUG Marche - June 2014

×