Wednesday, 14 December 2016

Java8 new features :- Consumer interface and start of lambda expression

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();

}

}

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...