Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functional Programming with Java

Similar presentations


Presentation on theme: "Functional Programming with Java"— Presentation transcript:

1 Functional Programming with Java
Advanced Programming in Java Functional Programming with Java Mehdi Einali

2 agenda Java versions Default method Functional interface Double colon
Functional programing Lambda expression Optional Streams

3 Java versions

4 Java versions-1 Version Release Date Main Features 1.0 1996
The first stable version 1.1 1997 an extensive retooling of the AWT event model Inner classes added to language Reflection I18n and Unicode Support 1.2(j2se) 1998 Swing Framework Collection Framework 1.3 2000 Java performance engine Hotspot added RMI added 1.4 2002 Regular Expression support Exception Handling high-level api added Non-Blocking IO(NIO 1) added Java Web Start

5 Java versions-2 1.6(J2SE 6) Version Release Date Main Features
22 update Generic Types Annotation Autoboxing Enumorations Varargs Foreach 1.6(J2SE 6) More than 50 updates Last one 113 Performance improvements Security Improvements JDBC 4 J2SE 7 2011-now Many update Last one 101 String in switch NIO 2 Diamond in generic types Concurrency high level API

6 Java versions-3 Java SE 9 Version Release Date Main Features Java SE 8
2014-now Last update 92 Lambda expression or closure (Functional Programming) JavaScript embedded emulator Annotation on Java Type default method in interface Java SE 9 Scheduled for  March 2017 Better support for multi-gigabyte heaps Java module system (OSGI like built-in system) Reactive Streams support Better support for parallel systems Java SE 10 Scheduled for  March 2018 Object without Identity Support 64-bit addressable arrays 

7 Default method in interfaces

8 Old interface

9 Default keyword

10 Default method In ‘the strictest sense’, Default methods are a step backwards because they allow you to ‘pollute’ your interfaces with code they provide the most elegant way to allow backwards compatibility The implementation will be used as default if a concrete class does not provide implementation for that method.

11 Notes on default default method can only access its arguments as interfaces do not have any state. Remember interface don’t have object state and its fields are implicitly public static final You can write static method in interface and call it from default method Both on them don’t depend on method arguments and class variables rather than object state All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.

12 Notes on default When we extend an interface that contains a default method, we can perform following: Not override the default method and will inherit the default method. Override the default method similar to other methods we override in subclass. Redeclare default method as abstract, which force subclass to override it

13 Conflicts multiple interface
Since classes in java can implement multiple interfaces, there could be a situation where 2 or more interfaces has a default method with the same signature hence causing conflicts as java will not know what methods to use at a time. 

14 Functional Programming

15 Java programing paradigms
Prior to JavaSE 8, Java supported three programming paradigms procedural programming object-oriented programming generic programming. Java SE 8 adds functional programming programming that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Stateless functions is building block of code

16 Why functional programing?
Multi core and multi thread processor increase every day Functions can pass as building block of computations. Distributes on threads Collections is every important data structure in algorithms In memory storage paradigms are increasing Collection operations is more frequent process consuming codes

17 collection iteration External iteration Disadvantages:
Developer code do iterations What you want to accomplish collection oriented task and how Disadvantages: Its error prone (keep all intermediate variables consistent) Duplicate logic In be optimized in library level Internal iterations Just say what you want to accomplish collection oriented task.

18 compare

19 Functional interface

20 Functional interface An interface that contains exactly one abstract method (and may also contain default and static methods).also known as single abstract method (SAM) interfaces Functional interfaces are used extensively in functional programming, because they act as an object oriented model for a function. To ensure that your interface meet the requirements, you should add the java.util.function package for basic functional interfaces

21 sample

22

23 Double colon

24 Double colon static method
Java 8 enables you to pass references of methods or constructors via the :: keyword

25 Double colon object method

26 Double colon constructor

27 Lambda expression

28 Lambda expressions Functional programming is accomplished with lambda expressions. A lambda expression represents an anonymous method—a shorthand notation for implementing a functional interface, similar to an anonymous inner class A lambda consists of a parameter list followed by the arrow token (->) and a body (parameterList) -> {statements} (int x, int y) -> {return x + y;}

29 syntax When the body contains only one expression, the return keyword and curly braces may be omitted, as in: (x, y) -> x + y in this case, the expression’s value is implicitly returned When the parameter list contains only one parameter, the parentheses may be omitted, as in value -> System.out.printf("%d ", value) () -> System.out.println("Welcome to lambdas!")

30 scoping Accessing outer scope variables from lambda expressions is very similar to anonymous objects. You can access effectively final variables from the local outer scope as well as instance fields and static variables. effectively final means: Explicitly final Implicitly final: not explicit final keyword but acts as final variable

31

32 optional

33 optional Optionals are not functional interfaces, instead it's a nifty utility to prevent NullPointerException. Optional is a simple container for a value which may be null or non-null. Think of a method which may return a non-null result but sometimes return nothing. Instead of returning null you return an Optional in Java 8.

34 sample

35 Streams

36 streams A java.util.Stream represents a sequence of elements on which one or more operations can be performed Stream operations are either intermediate or terminal.  terminal operations return a result of a certain type intermediate operations return the stream itself so you can chain multiple method calls in a row Stream operations can either be executed sequential or parallel.

37 sample

38

39 Parallel stream Streams can be either sequential or parallel.
Operations on sequential streams are performed on a single thread while operations on parallel streams are performed concurrent on multiple threads.

40 sample

41 end


Download ppt "Functional Programming with Java"

Similar presentations


Ads by Google