Insight into java 1.8, OOP VS FP

26
Insight into Java 1.8 “Java is still not dead—and people are starting to figure that out.” Prepared by: Syed Awais Mazhar [email protected]

Transcript of Insight into java 1.8, OOP VS FP

Insight into Java 1.8“Java is still not dead—and people are starting to figure that out.”

Prepared by: Syed Awais [email protected]

Agenda

Object Oriented Programming.

Functional Programming.

FP VS OOP

Java 8 Recent API’s

5 mins

Object Oriented

Programming.

5 mins

Functional

Programming.

5 mins

FP VS OOP

35 mins

Java 8 Recent API’s

- Lambda Expressions

- Method References

- Built-in Functional

Interfaces, etc.

15 mins

Java Collections and

Streams

Time Breakthrough

Object Oriented Programming

OOP is a programming paradigm based on the concept of "objects", which are data

structures that contain data, in the form of fields, often known as attributes; and code, in the

form of procedures, often known as methods.

When to choose object oriented approach?

When you have a fixed set of operations on things, and as your code evolves, you primarily

add new things. This can be accomplished by adding new classes which implement existing

methods, and the existing classes are left alone.

Functional Programming

Functional programming is a programming paradigm, a style of building the structure and

elements of computer programs, that treats computation as the evaluation of mathematical

functions and avoids changing-state and mutable data.

When to choose functional approach?

When you have a fixed set of things, and as your code evolves, you primarily add new

operations on existing things. This can be accomplished by adding new functions which

compute with existing data types, and the existing functions are left alone.

FP says that data and behavior

are distinctively different things

and should be kept separate

for clarity

Data is only loosely coupled to

functions.

Functions hide implementations

OOP says that bringing together

data and its behaviour in a

single location makes it easier

to understand how a program

works.

Data and operations are tightly

coupled.

Objects hide implementation of

operations from other objects.

Object Oriented Programming Functional ProgrammingVS

Central model of abstraction is

the function, not the data

structure

Central activity is writing new

functions, and composing

functions together.

Central model of abstraction is

the data itself.

Central activity is composing new

objects and extending existing

objects by adding new

methods

Object Oriented Programming Functional ProgrammingVS

Java 8

Default Methods for Interfaces

Functional Interfaces

Lambda expressions

Method References

Built-in Functional Interfaces

Streams

Why Lambdas?

Default Methods for Interfaces

Java 8 enables us to add non-abstract method implementations to interfaces by utilizing the

default keyword.

Functional Interfaces

An interface which contain exactly one abstract method declaration. Since default methods

are not abstract you're free to add default methods to your functional interface.

To ensure that your interface meet the requirements, you should add the @FunctionalInterface

annotation. The compiler is aware of this annotation and throws a compiler error as soon as

you try to add a second abstract method declaration to the interface.

Lambda - a formal definition

Anonymous function (also function literal or lambda abstraction) is a function definition that

is not bound to an identifier. Lambdas are often:

● Passed as an arguments to higher order functions.

Or

● Used to construct a result of a higher order function that needs to return a function.

Lambda expressions

Let's start with a simple example of how to sort a list of strings in prior versions of Java:

The static utility method Collections.sort accepts a list and a comparator in order to sort the

elements of the given list. You often find yourself creating anonymous comparators and

pass them to the sort method.

Lambda expressions

Instead of creating anonymous objects all day long, Java 8 comes with a much shorter

syntax, lambda expressions:

For one line method bodies you can skip both the braces {} and the return keyword. The java

compiler is aware of the parameter types so you can skip them as well.

Lambda Scopes

Accessing outer scope variables from lambda expressions is very similar to anonymous

objects. You can access final variables from the local outer scope as well as instance fields

and static variables.

But different to anonymous objects the variable num does not have to be declared final.But It is meant to be final, i.e we can not change the value of num.It will be compile time error if we do so.

Lambda Scopes

Accessing Default Interface Methods:

Default methods cannot be accessed from within lambda expressions. The following code

does not compile:

Method References

It is a feature which is related to Lambda Expression. It allows us to reference constructors

or methods without executing them. Method references and Lambda are similar in that they

both require a target type that consist of a compatible functional interface.

Built-in Functional Interfaces

The JDK 1.8 API contains many built-in functional interfaces. Some of them are well known

from older versions of Java like Comparator or Runnable. Those existing interfaces are extended

to enable Lambda support via the @FunctionalInterface annotation.

Predicates:

Predicates are boolean-valued functions of one argument. The interface contains various

default methods for composing predicates to complex logical terms (and, or, negate)

Built-in Functional Interfaces

Predicates:

Built-in Functional Interfaces

Functions:

Functions accept one argument and produce a result. Default methods can be used to chain

multiple functions together (compose, andThen).

Built-in Functional Interfaces

Suppliers:

Suppliers produce a result of a given generic type. Unlike Functions, Suppliers don't accept

arguments.

Consumers:

Consumers represents operations to be performed on a single input argument.

Built-in Functional Interfaces

Optionals:

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.

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. While 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. Streams are created on a source, e.g. a

java.util.Collection like lists or sets (maps are not supported).

Stream operations can either be executed sequential or parallel.

Streams

Collections in Java 8 are extended so you can simply create streams either by calling

Collection.stream() or Collection.parallelStream() . The following sections explain the most

common stream operations.

I. Filter II. Sorted

III. Map IV. Match

V. Count VI. Reduce

Parallel Streams

As mentioned above streams can be either sequential or parallel. Operations on sequential

streams are performed on a single thread while operations on parallel streams are

performed concurrently on multiple threads.

Let’s check the code example.

Lambda expressions

Why Lambdas?

Enables Functional Programming

Readable and concise code.

Easier to use API’s and Libraries.

Enable Support for parallel processing

References

1. https://www.slideshare.net/LivePersonDev/functional-programming-with-java-8

2. http://www.cogsys.wiai.uni-bamberg.de/schmid/uoshp/lehreuos/fp01-www/fp-referate/oo-vs-fp.pdf

3. http://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-

programming

4. https://hackpad.com/ep/pad/static/x4m38aCcrMs

5. http://winterbe.com/posts/2014/03/16/java-8-tutorial/

6. https://blog.idrsolutions.com/2015/02/java-8-method-references-explained-5-minutes/