FUNCTIONAL
– JAVA 8
Learn about Functional Programming
1
WHY JAVA 8 ?
• Most popular and widely accepted language in the world.
• Java creators wanted to introduce the Functional features such as:
• Lambdas
• Streams
• Optional and etc.,
• Technological advancements with the mobile/laptops/systems.
• New Java 8 features simplify the concurrency operations. 2
IMPERATIVE VS DECLARATIVE
PROGRAMMING
3
IMPERATIVE STYLE OF PROGRAMMING
• Focuses on how to perform the operations.
• Embraces Object mutability.
• This style of programming lists the step by step of instructions on how
to achieve an objective.
• We write the code on what needs to be done in each step.
• Imperative style is used with classic Object Oriented Programming.
4
DECLARATIVE STYLE OF PROGRAMMING
• Focuses on what is the result you want.
• Embraces Object immutability.
• Use the functions that are already part of the library to achieve an
objective.
• Functional Programming uses the concept of declarative programming.
5
IMPERATIVE VS DECLARATIVE PROGRAMMING
• Example 1: Sum of 100 numbers from 0 to 100.
/** • Shared mutable state
* Imperative Style - how style of programming and it will go step by
*/ step.
int sum = 0;
• It will have issues if
for (int i = 0; i <= 100; i++) { we try to run the code
sum += i; in a multithreaded
} environment.
System.out.println("Sum is : " + sum);
6
IMPERATIVE VS DECLARATIVE PROGRAMMING
• Example 1: Sum of 100 numbers from 0 to 100.
/**
* Declarative style. what style of programming. You let the system
do the job for you and get the result.
*/
int sum = IntStream.rangeClosed(0, 100).sum();
System.out.println("Sum is : " + sum);
7
IMPERATIVE VS DECLARATIVE PROGRAMMING
• Example 2: Removing duplicates from a list of integers.
/**
* Imperative Style
*/
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8,
9, 9);
List<Integer> uniqueList = new ArrayList<>();
for (Integer i : integerList)
if (!uniqueList.contains(i)) {
uniqueList.add(i);
}
System.out.println("unique List : " + uniqueList); 8
IMPERATIVE VS DECLARATIVE PROGRAMMING
• Example 2: Removing duplicates from a list of integers.
/**
* Declarative Style
*/
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8,9,
9);
List<Integer> uniqueList = integerList.stream().distinct().
collect(toList());
System.out.println("unique List : " + uniqueList);
9
WHAT IS FUNCTIONAL PROGRAMMING?
• Embraces creating Immutable objects.
• More concise and readable code.
• Using functions/methods from the built-in library.
• Write code using Declarative approach.
10
FUNCTIONAL PROGRAMMING - KEY CONCEPTS
❖ Functions as first class objects. public interface MyPrinter{
❖ i.e., we can create an "instance" public void print(String s);
of a function, just like a }
reference to a String, Map or any //Lambda Expression
other object.
MyPrinter myPrinter =
❖ Functions can also be passed as s -> System.out.println(s);
parameters to other functions.
11
FUNCTIONAL PROGRAMMING - KEY CONCEPTS
❖ Pure functions
❖ The return value of the function depends only on the input parameters
passed to the function.
public class PureFunctionExample{
public int sum(int a, int b) {
return a + b;
}
}
12
FUNCTIONAL PROGRAMMING - KEY CONCEPTS
❖ Higher Order Functions
❖ The function takes one or more functions as parameters. or
❖ The function returns another function as result.
//A function returning a lambda expression
public <T> IFactory<T> createFactory(IProducer<T> producer, IConfigurator<T>
configurator) {
return () -> {
T instance = producer.produce();
configurator.configure(instance);
return instance;
}
} 13
FUNCTIONAL PROGRAMMING - KEY CONCEPTS
❖ Immutable variables
❖ Immutable variables makes it easier to avoid side effects.
❖ Changing state outside of a function is referred to as a side effect.
❖ State outside of a function refers both to member variables in the class
or object outside the function,
❖ and member variables inside parameters to the functions, or state in
external systems like file systems or databases.
14
FUNCTIONAL PROGRAMMING - KEY CONCEPTS
❖ Favour recursion over looping
❖ Recursion uses function calls to achieve looping, so the code becomes more
functional.
15