JAVA 8 Features
JAVA 8 Features
Agenda
Lambda Expression
Functional Interfaces
Predicates
Functions
Lambda (λ) Expression
The other languages which uses lambda expressions are:
C#.Net
C Objective
C
C++
Python
Ruby etc.
and finally in java also.
The Main Objective of λ Lambda Expression is to bring benefits of functional programming into
java.
What is Lambda Expression (λ):
Lambda Expression is just an anonymous(nameless) function. That means the function which doesn’t have the
name, return type and access modifiers.
Ex: 3 public String str(String str) { (String str) -> return str;
return str; } (str) -> str;
Conclusions:
A lambda expression can have zero or more number of parameters(arguments).
Ex: () -> S.o.p(“hello”); (int a ) -> S.o.p(a); (int a, int b) -> return a+b;
Usually we can specify type of parameter. If the compiler expect the type based on the context then we can
remove type. i.e., programmer is not required.
Ex: (int a, int b) -> S.o.p(a+b); (a,b) -> S.o.p(a+b);
If multiple parameters present then these parameters should be separated with comma(,).
If zero number of parameters available, then we have to use empty parameter [ like ()]. Ex: () ->
sop(“hello”);
If only one parameter is available and if the compiler can expect the type, then we can remove the type and
parenthesis also.
Ex: (int a) -> S.o.p(a); (a) -> S.o.p(a); a -> S.o.p(a);
Once we write lambda expression we can call that expression just like a method, for this functional interfaces
are required.
Functional Interfaces:
if an interface contain only one abstract method, such type of interfaces are
called functional interfaces and the method is called functional method or single
abstract method(SAM).
Ex:
1) Runnable It contains only run() method
2) Comparable It contains only compareTo() method
3) ActionListener It contains only actionPerformed()
4) Callable It contains only call() method
Inside FunctionalInterface we have to take exactly only one abstract method.If we are
not declaring that abstract method then compiler gives an error message.
Ex: @FunctionalInterface
Interface Interf { compilation error
}
Functional Interface Vs Lambda Expressions:
Once we write Lambda expressions to invoke its functionality, then Functional Interface is
required. We can use Functional Interface reference to refer Lambda Expression. Wherever
Functional Interface concept is applicable there we can use Lambda Expressions
Functions are exactly same as predicates except that functions can return any
type of result.
Function interface also present in java.util.function package.
Functional interface contains only one method i.e., apply()
interface function(T,R) {
public R apply(T t);
}
Ex Write a function to find length of given input string.
import java.util.function.*;
class Test {
public static void main(String[] args) {
Function<String, Integer> f = s ->s.length();
System.out.println(f.apply(“Rahul"));
System.out.println(f.apply("Srivastava"));
}
}
Difference between predicate and function
Predicate Function
To implement conditional checks To perform certain operation And to
We should go for predicate return some result we Should go for
Predicate can take one type function
Parameter which represents Input Function can take 2 type Parameters.
argument type. Predicate<T> first one represent Input argument
type and Second one represent return
Predicate interface defines only
Type. Function<T,R>
one method called test()
Function interface defines only one
public boolean test(T t) Method called apply().
Predicate can return only boolean public R apply(T t)
value. Function can return any type of value
Any
Questions ?