1 / 75

Lambdas and Laughs

Lambdas and Laughs. Jim Bethancourt Houston JUG @ jimbethancourt. Forward Looking Statement. Ok, Not Really…. Topics Covered. Lambda Support Lambda syntax Interface enhancements Convert inner class to Lambda forEach & Streams Method & Constructor References Functional API.

Download Presentation

Lambdas and Laughs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lambdas and Laughs Jim Bethancourt Houston JUG @jimbethancourt

  2. Forward Looking Statement Ok, Not Really…

  3. Topics Covered • Lambda Support • Lambda syntax • Interface enhancements • Convert inner class to Lambda • forEach& Streams • Method & Constructor References • Functional API

  4. Download Links & IDE Support • Regular JDK download link • http://jdk8.java.net/download.html(for EAs) • Netbeans • IntelliJ • Eclipse

  5. Maven Support <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <!-- or higher – up to 3.1 now --> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>

  6. Lambdas A lambda expression is like a method: it provides a list of formal parameters and a body—an expression or block—expressed in terms of those parameters. • Expressions: • s -> s.length() • (int x, int y) -> x+y • () -> 42 Assign lambda to functional interface: Runnable r1 = () ->System.out.println("My Runnable");

  7. Lambdas Blocks: (x, y, z) -> { if (x==y) return x; else { int result = y; for (inti = 1; i < z; i++) result *= i; return result; }}

  8. Typical Use Cases • Anonymous classes (GUI listeners) • Runnables / Callables • Comparator • Apply operation to a collection via foreach method • Chain operations on a collection with Stream API

  9. MOAR !!! Monday Tuesday Jump-Starting Lambda [TUT3371]10:30 AM Hilton Yosimite B/C Lambda Under the Hood [CON4180]11 AM Hilton Imperial A Lambda Q&A Panel [CON3374]1:30 PM Hilton Yosimite B/C • Programming with Lambda Expressions in Java [CON1770]11 AM Hilton Imperial A • GS Collections and Java 8: Functional, Fluent, Friendly, and Fun [CON5423]11 AM Hilton Imperial B • Under the Hood of Java 8 Parallel Streams with an Oracle Solaris Dtrace [BOF1937] 9: 45 PM Moscone North 131

  10. MOAR!!! Thursday • Lambda Programming Laboratory [HOL3373]2 PM Hilton Franciscan A/B • Lambda-izingJavaFX [CON3248]3:30 PM Hilton Plaza A

  11. Effectively Final • For both lambda bodies and inner classes, local variables in the enclosing context can only be referenced if they are final or effectively final. • A variable is effectively final if its value is not reassigned after its initialization. • No longer need to litter code with final keyword

  12. Interface Defender Methods • Interface methods with bodies • default keyword • More graceful API evolution • Interfaces have no state • Static methods not inherited • Can reference abstract method • Called “Extended Interfaces” if no abstract methods present

  13. Super! • Extended Interfaces can extend other extended interfaces • Methods can be overridden • Can decorate parent definitions via super interface I1 { default void method1() {//do stuff}} interface I2 extends I1{ default voidmethod1() { super.method1(); //do new stuff }}

  14. Specify the Parent Interface interface D1{ default void meth1() {//do stuff}} interface D2extends D1{ void default meth1() { super.method1(); //do new stuff}} interface D3extends D1{ void default meth1() { super.method1(); //do new stuff}} interface D4extends D2, D3{ void default meth1() { D2.super.method1(); //do new stuff}}

  15. Convert Anonymous Class to Lambdafrom http://learnjavafx.typepad.com/weblog/2013/02/mary-had-a-little-%CE%BB.html // Anonymous inner class for event handling .onAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) {anim.playFromStart(); } })

  16. Convert Anonymous Class to Lambda • .onAction((ActionEvent) -> {anim.playFromStart(); } }) • .onAction((e) -> {anim.playFromStart();})

  17. Convert Anonymous Class to Lambda • .onAction((ActionEvent) -> {anim.playFromStart(); } }) • .onAction((e) -> {anim.playFromStart();}) • .onAction(e -> {anim.playFromStart(); }) • .onAction(e -> anim.playFromStart();)

  18. MOAR!!! • Transforming Code to Java 8 [CON1772] Thursday 2:30 Hilton Imperial B

  19. forEach • forEach() - available on Iterator & Map interfaces and their implementations • Allows for internal control of iteration of elements for possible parallel operation List<String> names =Arrays.asList(“Bill", “Ed", “Al"); names.forEach(e ->{System.out.println(e);}); But be careful!!!

  20. MOAR!!! • GS Collections and Java 8: Functional, Fluent, Friendly, and Fun [CON5423]Monday 11 AM Hilton Imperial B • Autumn Collections: From Iterable to Lambdas, Streams, and Collectors [TUT3472]Tuesday 8:30 AM Hilton Yosemite A • New Tricks for Old Dogs: Collections in Java 8 [CON6309] Thursday 4 PM Hilton Imperial A

  21. java.util.stream • Classes to support functional-style operations on streams of values • Stream<T> - A sequence of elements supporting sequential and parallel bulk ops

  22. java.util.stream • Stream opened by calling • Collection.stream() • Collection.parallelStream() List<String> names = Arrays.asList("Bill", “Ed", “Al"); out(names.stream().filter(e -> e.length() >= 4 ) .findFirst().get()); Returns “Bill”

  23. java.util.stream • All other interfaces in stream package accessible through Stream interface • Collector<T,R> - A (possibly parallel) reduction operation. • FlatMapper<T,U> - Maps element of type T to zero or more elements of type U.

  24. MOAR!!! • Journey’s End: Collection and Reduction in the Stream API [TUT3836]Monday 8:30 AM Hilton Yosemite A • Programming with Streams in Java 8 [CON1771] Monday 4 PM Hilton Imperial A • Parallel Streams Workshop [CON3372]Wednesday 10 AM Hilton Yosemite A • Loads more!!!

  25. java.util • Spliterator<T> provides traversal operations • Optional<T> • Returned by Stream’s aggregate methods find*(), reduce(), min(), max() • Call get() to get the value it’s holding

  26. Method & Constructor References • A method reference is used to refer to a (static or instance) method without invoking it • Aconstructor reference is similarly used to refer to a constructor without creating a new instance of the named class or array type. • Specified with the :: (double colon) operator

  27. Method & Constructor References • Provide a way to refer to a method /constructor without invoking it • Examples: System::getProperty "abc"::length String::length super::toString ArrayList::new int[]::new

More Related