Functional Programming in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Functional Programming in Java

Functional programming is a subset of the Declarative programming paradigm.


Functional programming technique makes our code more concise, readable, and
predictable.
Introduction to Programming Paradigm

Programming Paradigm is a way or style of programming. The programming paradigm can


be classified into two types:

 Imperative programming paradigm


 Declarative programming paradigm

Imperative Programming Paradigm

Imperative programming paradigm consists of a sequence of statements that changes the


program's state until the target result is achieved. The main focus is on how to achieve the
goal. It consists of three main programming approaches:

 Procedural programming
 Object Oriented programming
 Parallel Processing Approach

public class Main {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 5; i ++) {

sum += i;

System.out.println(sum);

Declarative Programming Paradigm

Declarative programming is a paradigm in which we define what needs to be accomplished


without defining how it has to be implemented. In the declarative programming paradigm, for
the same input arguments, the program produces the same result. The order of execution of
statements is not important in the declarative programming paradigm. It consists of three
main programming approaches:

 Logic programming paradigm


 Functional programming
 Database processing approach

import java.util.Arrays;

public class Main {

public static void main(String[] args) {

int[] array = new int[] {1, 2, 3, 4, 5};

int[] evenArray = Arrays.stream(array)

.filter(a -> a % 2 == 0)

.toArray();

System.out.println(Arrays.toString(evenArray));

In this example, the input array is converted to stream. The filter() method is invoked for
each value in the stream and it is evaluated with the expression a -> a % 2 == 0.

filter(1) => false


filter(2) => true
filter(3) => false
filter(4) => true
filter(5) => false

The filter() method passes the value to the next operation i.e. toArray() only if the expression
inside it evaluates to true. So, in this case, only 2 and 4 are passed to toArray() operation, and
hence the output is [2, 4].

Here, we didn't define the steps to filter the even numbers. Instead, we told the compiler what
we want via the expression filter(a -> a % 2 == 0).

What is Functional Programming in Java?


Functional programming is a paradigm where the basic unit of computation is a
function.
Here functions are not the methods we write in programming. Methods are
procedures where we write a sequence of instructions to tell the computer what
to do. In functional programming, functions are treated like mathematical
functions where we map inputs to outputs to produce a result. Eg.

f(x)=3x.
In functional programming, the software is developed around the evaluation of
functions.
The functions are isolated and independent, and they depend only on the
arguments passed to them and do not alter the program's state like modifying a
global variable.

You might also like