Functional Programming in Java

The Java 8 promotes functional programming and base is taken from Lamda Expression and they promote these -

Pure function - Completely Independent

Immutability – No change in internal state

First Class functions – use function as value

Higher-order functions – takes function as parameter

 

Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.

The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.

Java lambda expression is treated as a function, so compiler does not create .class file.

Java lambda expression is consisted of three components and related to syntax

 

Argument-list: It can be empty or non-empty as well.

Arrow-token: It is used to link arguments-list and body of expression.

Body: It contains expressions and statements for lambda expression.

 

No Parameter Syntax

() -> {  

}  

 

One Parameter Syntax

(p1) -> {  

 

}  

 

Two Parameter Syntax

(p1,p2) -> {  

}  

 


1 comment: