Lamda & Stream API
java 8
Functional Progarmming
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.
It is adeclarative programming paradigm, which means programming is done with expressions
Imperative + mutability
vs
Declarative+
Immutability
Anonymous Inner Class
new Thread(
new Runnable(){
public void run(){
System.out.println("I am another thread!");
}
}
).start();
interface Runnable{
public void run();
}
Functional Interface
interface Runnable{
public void run();
}
Interface with exactly one method
interface Predicate<T> {
boolean test(T t);
}
Predicate
using Anonymus class as function ...
void eligibleForDriving(){
matchPerson(
new Predicate<Person>(){
public boolean test(Person p){
return p.getAge() >= 16;
}
}
)
}
void eligibleForDriving(){
matchPerson( new Predicate<Person>(){ public boolean test(Person p){ return p.getAge() >= 16; } }
)
}
Lamda Expressions
{ who -> the hell is who }
void eligibleForDriving(){
matchPerson(p -> p.age > 16);
}
matchTwoPerson((p1,p2)-> p2.age-p1.age);
matchTwoPerson((p1,p2)->{
int result;
result = p2.age - p1.age;
return result;
});
Different Variants ....
What is Lamda ?
- An anonymous Function
- allows one to treat code as data
- parameterize behaviour
- provides closures
- productive, flexible, "fluent" style programming
void eligibleForDriving(){
matchPersons(p -> p.getAge() >= 16);
}
Lamda expressions are converted to instance of functional interface
Stream API
Thanks !!!
Feedbacks please
deck
By Nikunj Parmar
deck
- 843