java8 released: march 18, 2014. lambda expressions

Download Java8 Released: March 18, 2014. Lambda Expressions

If you can't read please download the document

Upload: magnus-nichols

Post on 17-Jan-2018

230 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Java8 Released: March 18, 2014 Lambda Expressions 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 How did Java deal with functional programming prior to Java8 See imperativeVersusFunctional Another example of anon class 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 Another example 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 How to create a Functional Interface Methods overwritten from Object dont count, so this is See FunctionalInterfaceTest Location of Functional Interfaces You may define your own Functional Interfaces Java8 defines many in: java.util.function.* 43 interfaces in 4 categories 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 Is a Lambda expression an Object? Not really. It is an object without identity. It does not inherit from Object and so you cant call the.equals(),.hashcode(), etc. There is no new keyword in lamba, so there is far less overhead both at compile- and run- time. Streams 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 Iterator See IteratorDriver 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 4 categories of Functional Interfaces 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 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 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 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 New Time API in Java8 From pluralsight.com