wednesday, march 27, 13 · • java.util.function – functional interfaces • various...

19
Wednesday, March 27, 13

Upload: others

Post on 14-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Wednesday, March 27, 13

Page 2: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Accelerated Lambda Programming

Stuart MarksJDK Core Libraries

@stuartmarks

Wednesday, March 27, 13

Page 3: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Outline

• Lambda is a useful new Java 8 feature

• Becomes much more useful if there are libraries that use it

• Today’s emphasis: the new streams library

• Streams library features illustrated using code examples

• After we dispense with a few slides...

Wednesday, March 27, 13

Page 4: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Streams

• Stream: an abstraction representing a multiplicity of values

• Contrast with InputStream/OutputStream: sequence of bytes

• Zero or more, or even unbounded

• Unlike collections, not (necessarily) stored anywhere

• Might or might not be ordered

• Can be sequential or parallel

• Values may be objects or int, long, double primitives

• Emphasis of objects-as-values over mutation

Wednesday, March 27, 13

Page 5: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Pipelines

• A pipeline consists of:

• A source

• Zero or more operations

• A destination (or terminal operation)

• Operations connected by streams

• Laziness-seeking

• Sequential or parallel

Wednesday, March 27, 13

Page 6: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

How Are Lambdas Involved?

Terminal OpSource Op Op Op

Lambda Lambda Lambda Lambda

Stream

Wednesday, March 27, 13

Page 7: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Let’s look at some code!

Wednesday, March 27, 13

Page 8: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Summation by Mutation

Experience  is  simply  the  name  we  give  our  mistakes.

         10  +    2  +    6  +      3  +    4  +  2  +  4  +    3    +      9

         12

Wednesday, March 27, 13

Page 9: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Summation by Mutation

Experience  is  simply  the  name  we  give  our  mistakes.

         10  +    2  +    6  +      3  +    4  +  2  +  4  +    3    +      9

         12          18          21          25          27          31          34          43

Wednesday, March 27, 13

Page 10: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

         10  +    2  +    6  +      3  +    4  +  2  +  4  +    3    +      9

Summation by Reduction

Experience  is  simply  the  name  we  give  our  mistakes.

Wednesday, March 27, 13

Page 11: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

         10  +    2  +    6  +      3  +    4  +  2  +  4  +    3    +      9

Summation by Reduction

Experience  is  simply  the  name  we  give  our  mistakes.

+  0

12 9 6 7 9

Wednesday, March 27, 13

Page 12: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

         10  +    2  +    6  +      3  +    4  +  2  +  4  +    3    +      9

Summation by Reduction

Experience  is  simply  the  name  we  give  our  mistakes.

+  0

12 9 6 7 9 +  0

21 13 9

Wednesday, March 27, 13

Page 13: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

         10  +    2  +    6  +      3  +    4  +  2  +  4  +    3    +      9

Summation by Reduction

Experience  is  simply  the  name  we  give  our  mistakes.

+  0

12 9 6 7 9 +  0

21 13 9 +  0

34 9

Wednesday, March 27, 13

Page 14: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

         10  +    2  +    6  +      3  +    4  +  2  +  4  +    3    +      9

Summation by Reduction

Experience  is  simply  the  name  we  give  our  mistakes.

+  0

12 9 6 7 9 +  0

21 13 9 +  0

34 9

43Wednesday, March 27, 13

Page 15: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Now, back to the code!

Wednesday, March 27, 13

Page 16: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Summary

• Project Lambda covers a lot of territory

• Changes to JVM, language, compiler, and libraries

• Library enhancements in support of Lambda

• java.util.stream – new “streams” library

• java.util.function – functional interfaces

• various enhancements to collections (mainly default methods)

• several “point lambdafications” around the class libraries

• Introduce a functional programming style to Java

• (insert joke about dysfunctional style here)

Wednesday, March 27, 13

Page 17: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

References

• Main Project Lambda pagehttp://openjdk.java.net/projects/lambda/

• Project Lambda Buildshttp://jdk8.java.net/lambda

• Maurice Naftalin’s Lambda FAQhttp://www.lambdafaq.org

• NetBeans builds with Java 8 & Lambda supporthttp://wiki.netbeans.org/JDK8

Wednesday, March 27, 13

Page 18: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

The  preceding  is  intended  to  outline  our  general  product  direction.  It  is  intended  for  information  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver  any  material,  code,  or  functionality,  and  should  not  be  relied  upon  in  making  purchasing  decisions.  The  development,  release,  and  timing  of  any  features  or  functionality  described  for  Oracle’s  products  remains  at  the  sole  discretion  of  Oracle.

Disclaimer

Wednesday, March 27, 13

Page 19: Wednesday, March 27, 13 · • java.util.function – functional interfaces • various enhancements to collections (mainly default methods) • several “point lambdafications”

Q&A

Wednesday, March 27, 13