Java 8
What’s new?
Onofrio Panzarino @onof80
JUG Marche - June 2014
Agenda
1. Lambda expressions
2. Stream API
3. Parallel operations
4. Default methods
5. Date time API
6. Optional: null is...
Lambda expressions
(lambda (x) (+ x 1)) LISP
function (x) { return x + 1; }; JavaScript
[](auto x) { return x + 1; }; C++1...
Java’s Lambdas
Syntax: (arguments) -> { body }
Collections.sort(employees,
(Employee p1, Employee p2) ->
p1.getSurname().c...
Stream API
On collections:
all.stream()
.filter(e -> e.getRole().equalsIgnoreCase(role))
.sorted(Comparator.comparing(e ->...
Parallel Streams
Very difficult:
all.parallelStream()
.filter(e -> e.getRole().equalsIgnoreCase(role))
.sorted(Comparator....
Default methods
public interface Person {
String getName();
String getSurname();
default String getFullName() {
return get...
New Dates API
● LocalDate, LocalTime and LocalDateTime
● ZonedDateTime
● Period vs Duration
● Chronologies
http://www.orac...
Optional
.isPresent(); // (boolean)
.get();
.orElse(defaultValue);
.orElseGet( () -> new Employee(...) );
.orElseThrow( ()...
CompletableFuture
CompletableFuture<String> future = CompletableFuture
.supplyAsync(() -> "Hello future!");
future.handle(...
CompletableFuture
Future composition!
CompletableFuture<Stream<String>> future1 = CompletableFuture
.supplyAsync(() -> rep...
References
http://www.oracle.com/technetwork/java/javase/
8-whats-new-2157071.html
http://www.informit.com/articles/articl...
Open Discussion
Thank you!
Onofrio Panzarino @onof80
JUG Marche - June 2014
Upcoming SlideShare
Loading in …5
×

Jug Marche: Meeting June 2014. Java 8 hands on

608 views

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.

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

  • Be the first to like this

No Downloads
Views
Total views
608
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Jug Marche: Meeting June 2014. Java 8 hands on

  1. 1. Java 8 What’s new? Onofrio Panzarino @onof80 JUG Marche - June 2014
  2. 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. 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. 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. 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. 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. 7. Default methods public interface Person { String getName(); String getSurname(); default String getFullName() { return getSurname() + " " + getName(); } } Multiple inheritance ? -> Must implement the method explicitly
  8. 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. 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. 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. 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. 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. 13. Open Discussion Thank you! Onofrio Panzarino @onof80 JUG Marche - June 2014

×