Your SlideShare is downloading. ×
0
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Java 8
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java 8

829

Published on

http://jsug.at/wiki/Meeting_52

http://jsug.at/wiki/Meeting_52

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
829
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
1
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 . Build #84 Robert Bachmann JSUG Meeting #52 1
  2. Outline • Interface additions • Lambda Syntax • Library additions java.util.function java.time java.util.Optional java.util.streams 2
  3. Do try this at home • JDK8, http://jdk8.java.net/download.html • Optional: IntelliJ IDEA 12.1 3
  4. Interface additions • Can add static methods to interfaces • Can add default methods to interfaces • Functional interfaces: An interface with one abstract method 4
  5. Examples package java.util; public interface List<E> extends Collection<E> { /* [...] */ default void sort(Comparator<? super E> c) { Collections.sort(this, c); } } 5
  6. Examples package java.util.function; @FunctionalInterface public interface Consumer<T> { void accept(T t); } 6
  7. Examples public interface Iterable<T> { Iterator<T> iterator(); default void forEach( Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } } 7
  8. Default method rules • Speci c interface over parent interface • Object method over default method • Can not provide toString, hashCode, equals 8
  9. Lambda syntax • Used in conjunction with functional interfaces • Alternative to anonymous inner classes 9
  10. Examples List<String> list = Arrays.asList(”a”,”b”); list.forEach(new Consumer<String>() { // Java 7 public void accept(String s) { System.out.println(s); } }); // Java 8 list.forEach( s -> {System.out.println(s);} ); list.forEach( s -> System.out.println(s) ); list.forEach( System.out::println ); 10
  11. Examples List<Person> personList = ...; // Java <= 7 Collections.sort(personList, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return Long.compare(p1.getAge(), p2.getAge()); } }); 11
  12. Examples List<Person> personList = ...; // Java 8 personList.sort( (p1,p2) -> { return Long.compare(p1.getAge(), p2.getAge()) }); // or: personList.sort( (p1, p2) -> Long.compare(p1.getAge(), p2.getAge()) ); // alternative approach: personList.sort( Comparators.comparing(Person::getAge)); 12
  13. java.util.function (1/2) • • • • • Consumer (T → void) Supplier (void → T) Predicate (T → boolean) Function (T → R) and UnaryOperator (T → T) BiFunction, BiPredicate, BinaryOperator 13
  14. java.util.function (2/2) • • • • • • • • IntConsumer (int → void) IntSupplier (void → int) IntPredicate (int → boolean) IntFunction (int → R) IntUnaryOperator (int → int) ToIntFunction (T → int) ObjIntConsumer, ToIntBiFunction Same for Double and Long 14
  15. java.time • • • • • Similar to JodaTime Instant & Clock LocalDateTime, LocalDate, LocalTime OffsetDateTime, OffsetTime ZonedDateTime 15
  16. java.util.Optional Optional<String> name = f(); // example 1 System.out .println(”Hello␣” + name.orElse(”user”)); // example 2 name .ifPresent( s -> System.out.println(”Hello␣” + s) ); 16
  17. java.util.stream // JDK example int sumOfWeights = blocks.stream() .filter(b -> b.getColor() == RED) .mapToInt(b -> b.getWeight()) .sum(); // Example with personList personList // List<Person> .stream() // Stream<Person> .filter(p -> p.getAge() >= 18) // Stream<Person> .map( p -> p.getName() ) // Stream<String> .forEach(System.out::println) // void ; 17
  18. Further reading • • • http://www.techempower.com/blog/2013/03/26/everythingabout-java-8/ http://javadocs.techempower.com/jdk18/api/overviewsummary.html src.zip in your JDK 8 installation 18
  19. Questions? 19
  20. Thanks Twitter @robertbachmann Email rb@ — .at 20

×