Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java8 Released: March 18, 2014. Lambda Expressions.

Similar presentations


Presentation on theme: "Java8 Released: March 18, 2014. Lambda Expressions."— Presentation transcript:

1 Java8 Released: March 18, 2014

2 Lambda Expressions

3 Imperative versus Declarative Imperative: is a style of programming where you program the algorithm with control flow and explicit steps. Declarative: is a style of programming where you declare what needs be done without concern for the control flow. Functional programming: is a declarative programming paradigm that treats computation as a series of functions and avoids state and mutable data to facilitate concurrency. “Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate computability, the Entscheidungsproblem, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus”. Wikipedia See BigDecimalMain

4 How did Java deal with “functional” programming prior to Java8 See imperativeVersusFunctional

5 Another example of anon class

6 Our first Lambda expression 1/ start with a “functional interface” - a functional interface has ONE abstract method. 2/ create an anonymous class and implement its method 3/ use the format as seen above 4/ no need for return statements, that's implied

7 Another example

8

9 Type of Lambda Expression The java ‘Type’ of a lamba expression is a “Functional Interface” Functional Interface is an interface with only ONE abstract method The concept of “Functional Interface” is new, but many of the interfaces in Java7 and before are “functional interfaces” such as Runnable, Comparable, etc. Java8 defines many more functional interfaces in the java.util.function.* pacakge. Older functional interfaces have been decorated with default methods to preserve backwards compatibility

10 How to create a Functional Interface Methods overwritten from Object don’t count, so this is a @FunctionalInterface See FunctionalInterfaceTest

11 Location of Functional Interfaces You may define your own “Functional Interfaces” Java8 defines many in: java.util.function.* 43 interfaces in 4 categories

12 Can I store a Lamba Expression in a variable in Java? Yes! Wherever a method requires an anonymous inner class (with one method), you may put a lamba expression. For example: Collections.sort(list, compL); You may also use lambda expressions with Streams See functionalIterfaceTest

13 Is a Lambda expression an Object? Not really. It is an ‘object without identity’. It does not inherit from Object and so you can’t call the.equals(),.hashcode(), etc. There is no ‘new’ keyword in lamba, so there is far less overhead both at compile- and run- time.

14 Streams

15 What is a Stream A stream is a limitless iterator that allows you to process collections. You can chain operations. This chain is called a pipeline. You must have at least one terminal operation and zero or more intermediary operations. The pipeline usually takes the form of map-filter-reduce pattern. Any stream operation that returns a Stream is an intermediate operation, and any object that returns void is terminal. Intermediate operations are lazy, whereas terminal operations are eager. A terminal operation will force the stream to be “spent” and you can NOT re-use that reference, though you can always get a new stream. May be parallelized and optimized across cores. See StreamMain

16 Iterator See IteratorDriver

17 4 categories of Functional Interfaces Functional Interface archetypes Example used in Consumer forEach(Consumer), peek(Consumer) Predicatefilter(Predicate) Functionmap(Function) Supplierreduce(Supplier) collect(Supplier) See java.util.function.* ConsumerMain

18 4 categories of Functional Interfaces

19 Method references You can refer to static or non-static method simply by using the double colon (method reference) notation like so: System.out::println stringLenComparator::compare See MethodRefsMain

20 Map is a transform The.map() method is not an associative data structure, rather it is a transformative operation. It takes a Stream and it returns either a Stream or Stream. Example: Stream to Stream

21 Intermediary operation Intermediary operation: A method that takes a Stream and returns a Stream. They are lazily loaded and will only be executed if you include a terminal operation at the end. – The peek() method – The map() method – The filter() method

22 Terminal operation Terminal operation: A method that takes a Stream and returns void. They are eagerly loaded and will cause the entire pipeline to be executed. A terminal operation will “spend” the stream. – The forEach() method – The count() method – The max() method – The collect() method – The reduce() method

23 New Time API in Java8

24 From pluralsight.com

25

26

27

28

29

30

31

32

33

34

35


Download ppt "Java8 Released: March 18, 2014. Lambda Expressions."

Similar presentations


Ads by Google