Tuesday, April 22, 2014

Java8 - Lambda Expressions - Example 1

Posted by Sharath Chandra
Hello Friends 

As the Java 8 SDK released during mid of March - I started exploring the Java 8 and it's one of the major updates to the core i.e. Lambda Expressions which is the hot topic/trend in the Java. 

Here first of all I am explaining a simple Lambda Expression - Hello World Program .. But the in detail will soon publish a pdf file along with all the changes and my understandings in a document. 

Step 1: 

Create a Functional Interface using @FunctionalInterface annotation tag which got introduced in Java 8 

@FunctionalInterface
public interface WorkerInterface {
     public void doSomeWork();
 }

Step 2: 
In the general class implement the above interface or no need of implement also. 


public class CustomizedLambdaExample {
 
    public static void callMethod(ExampleFunctionInterface funcInt) {
        funcInt.methodOne();
    }
    public static void main(String [] args) {
         //using Annonymous class - Prior to Java8
        callMethod(new ExampleFunctionInterface() {
            @Override
            public void methodOne() {
                System.out.println("methodOne called, using Anonymous class");
            }
        });
     
        //using Lambda expression - Using Java8 Lambda 
        callMethod( () -> System.out.println("Hello World") );
    }
}


STAY TUNE FOR THE COMPLETE REFERENCE FOR JAVA 8 and LAMBDA EXPRESSIONS

KEEP SHARING - BECAUSE IN KNOWLEDGE 
SHARING IS GAINING