Consumer interface :- Below you can observe different ways of implementing interface and how consumer interface is used to implement lambda expressions.
interface A {
public void show();
}
class B implements A {
@Override
public void show() {
System.out.println("Typical way of show");
}
}
public class LambdaDemo {
public static void main(String[] args) {
//Approach1 of implementing interface
B b = new B();
b.show();
//Approach2 of implementing interface
new A() {
public void show() {
System.out.println("smart way of show :-"+(1*0.1==0.1));
}
}.show();
//Approach3 of implementing using consumer interface
A obj;
obj = () -> {
System.out.println("show lambda");
};
obj.show();
A obj1;
obj1 = () -> System.out.println("show lambda without braces");
obj1.show();
}
}
interface A {
public void show();
}
class B implements A {
@Override
public void show() {
System.out.println("Typical way of show");
}
}
public class LambdaDemo {
public static void main(String[] args) {
//Approach1 of implementing interface
B b = new B();
b.show();
//Approach2 of implementing interface
new A() {
public void show() {
System.out.println("smart way of show :-"+(1*0.1==0.1));
}
}.show();
//Approach3 of implementing using consumer interface
A obj;
obj = () -> {
System.out.println("show lambda");
};
obj.show();
A obj1;
obj1 = () -> System.out.println("show lambda without braces");
obj1.show();
}
}
No comments:
Post a Comment