1)LAMDA EXPRESSIONS
2)Functional Interface
3)Default Method Interface
4)Static Method Interface
5)Pre defined Functional Interface
I)Predicates
II)Function
III)Consumer
iV)supplier
6)Stream API
7)Method Reference or Constructor Reference (::)
1)LAMDA EXPRESSIONS:-
Lambda expression is anonymous function which have set of parameters and a lambda (->) and a function body .You can call it function without name.
syntax:- Anonymous function() -> {function Body}
Uses
Readability :- easy to read
Encouragement of functional programming
Easy to write
Reduce code length
public class ThreadTest {
public static void main(String[] args) {
Runnable r1 = new Runnable() { // Anonymous class
@Override
public void run() {
System.out.println(“Using Anonymous class”);
}
};
Runnable r2 = () -> { // lambda expression
System.out.println(“Using Lambda Expression”);
};
new Thread(r1).start();
new Thread(r2).start();
}
}
Using Anonymous class
Using Lambda Expression
Functional Interface
SAM:- Single Abstract Method
@FunctionalInterface
interface MyInterfaceParam {
void m1(int a, int b);
}
package interview_java8;
interface MyInterface
{
void m1();}
public class LamdaExpression {
public static void main(String[] args) {
MyInterface i=()->{
System.out.println(“HI Sardar”);
};
i.m1();
}
}
Lamda using With Parameters
package interview_java8;
@FunctionalInterface
interface MyInterfaceParam {
void m1(int a, int b);}
public class LamdaExpressionParam {
public static void main(String[] args) {
MyInterfaceParam i = (a, b) -> {
System.out.println(“add–” + (a + b));
};
i.m1(10, 15);
}}
o/p: add==25
3)Default Method Interface
->In interface we can define default method implementation
->In Functional Interface we can define multiple default methods
And static methods
package interview_java8;
interface Left {
// void m1(int a, int b);
default void m2() {
System.out.println(“default Left”);
}}
interface Right {
// void m1(int a, int b);
default void m2() {
System.out.println(“default Right”);
}
}
public class DefaultMethodInterface implements Left, Right {
public static void main(String[] args) {
DefaultMethodInterface data = new DefaultMethodInterface();
data.m2();
}@Override
public void m2() {
System.out.println(“My Left”);
Left.super.m2();
}}
o/p
my left
default Left
4)Static Method Interface
->Overriding concept is not applicable
->it is used for Utility
package interview_java8;
interface StaiticLeft {
static void m2() {
System.out.println(“default Left”);
}
}public class StaticMethodInterface {
public static void main(String[] args) {
StaticMethodInterface c=new StaticMethodInterface();
//c.m2(); getting error
StaiticLeft.m2();
}
}
Pre defined Functional Interface
I)Predicates
II)Function
III)Consumer
IV)Supplier
I)Predicates
1)Sorting
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TestCompartor
{
public static void main(String[] args)
{
List<Integer> hh=null;
List<Integer> l=new ArrayList<>();
l.add(new Integer(0));
l.add(new Integer(55));
l.add(new Integer(20));
l.add(new Integer(15));
l.add(new Integer(33));
final List<Integer> sortedCategories = l.stream().sorted((o1, o2) -> o1.compareTo(o2)).collect(Collectors.toList());
System.out.println(sortedCategories);
}
}
output :-[0, 15, 20, 33, 55]
2)Sorting based on two attributes
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.jsoup.helper.StringUtil;
public class CustomCategory
{
Integer orderBy;
String code;
public CustomCategory()
{
}
public CustomCategory(Integer orderBy, String code)
{
this.orderBy = orderBy;
this.code = code;
}
public Integer getOrderBy()
{
return orderBy;
}
public void setOrderBy(Integer orderBy)
{
this.orderBy = orderBy;
}
public String getCode()
{
return code;
}
public void setCode(String code)
{
this.code = code;
}
List<CustomCategory> getAllCate()
{
List<CustomCategory> allcat=new ArrayList<CustomCategory>();
CustomCategory c1=new CustomCategory(1,”101″);
CustomCategory c2=new CustomCategory(5,”102″);
CustomCategory c=new CustomCategory(2,”104″);
CustomCategory c3=new CustomCategory(null,null);
CustomCategory c4=new CustomCategory(6,”103″);
CustomCategory c5=new CustomCategory( 0,”105″);
CustomCategory c6=new CustomCategory(9,”107″);
allcat.add(c1);
allcat.add(c2);
allcat.add(c3);
allcat.add(c4);
allcat.add(c5);
allcat.add(c6);
return allcat;
}
@Override
public String toString()
{
return “CustomCategory [orderBy=” + orderBy + “, code=” + code + “]”;
}
public static void main(String[] args)
{
CustomCategory category=new CustomCategory();
List<CustomCategory> alllist=category.getAllCate();
System.out.println(“before”+alllist);
Collections.sort(alllist, Comparator.comparing(CustomCategory::getOrderBy,
Comparator.nullsFirst(Comparator.naturalOrder())).thenComparing(CustomCategory::getCode,Comparator.nullsFirst(Comparator.naturalOrder())));
System.out.println(“after”+alllist);
}
}
output:-
//before[CustomCategory [orderBy=1, code=101], CustomCategory [orderBy=5, code=102], CustomCategory [orderBy=null, code=null], CustomCategory [orderBy=6, code=103], CustomCategory [orderBy=0, code=105], CustomCategory [orderBy=9, code=107]]
//after[CustomCategory [orderBy=null, code=null], CustomCategory [orderBy=0, code=105], CustomCategory [orderBy=1, code=101], CustomCategory [orderBy=5, code=102], CustomCategory [orderBy=6, code=103], CustomCategory [orderBy=9, code=107]]