Sunday, 21 February 2016

Using Lambda expressions from Java 8 - Part 2

Background

In last post we say what is Lambda expression and how we can use it. In this post we will see some advance topics related to Lambda expression like - 
  • Generic Functional Interfaces
  • Passing Lambda expressions as method arguments
  • Exception handling in Lambda expressions

Generic Functional Interfaces

lambda expression themselves cannot have generics but the functional interface that corresponds to a lambda expression can have generics.

Consider following demo code- 

public class HelloWorld {
    
    public static void main(String args[]) {
        
        MyInterface<String> myStringInterface = (input) -> "Hello " + input;
        MyInterface<Integer> myIntInterface = (input) -> 10 + input;
        
        System.out.println("String based lambda exp : " + myStringInterface.myFunc("OSFG"));
        System.out.println("Integer based labmda exp : " + myIntInterface.myFunc(14));
    }
}

interface MyInterface<T> {
    T myFunc(T t);
}


And the output is -
String based lambda exp : Hello OSFG
Integer based labmda exp : 24


Notice how same method is made generic to operate on both String and Integer inputs.

Passing Lambda expressions as method arguments

Lambda expressions can be sent as method arguments. This makes Java more powerful as you can now send executable code as a part of method argument. Consider following demo code -

public class HelloWorld {
    
    public static void main(String args[]) {
        
        System.out.println("Welcome Lambda : " + executeMyFunc((input)-> "Welcome " + input,"OSFG"));
        System.out.println("Hello Lamda : " + executeMyFunc((input)-> "Hello " + input,"OSFG"));
    }
    
    public static String executeMyFunc(MyInterface myInterface, String input) {
        return myInterface.myFunc(input);
    }

}

interface MyInterface {
    String myFunc(String t);
}

and the output is - 

Welcome Lambda : Welcome OSFG
Hello Lamda : Hello OSFG

Exception handling in Lambda expressions

A lambda expression can throw an exception just like any other method. However if that exception is a checked exception then it should be compatible with the exceptions listed in the throws clause of the abstract method in the corresponding functional interface.

Consider following demo code - 

public class HelloWorld {
    public static void main(String args[]) throws MyException {        
        MyInterface myInterface = (input) -> {
            if(input == null) {
                throw new MyException();
            }
            return "Hello World";
        };
    }
}

interface MyInterface {
    String myFunc(String t) throws MyException;
}


class MyException extends Exception {
    public MyException() {
        super("MyException");
    }
}


NOTE : Here if you abstract method does not throw the checked exception code will fail to compile.

That's it. Let me know if you have any questions on Lambda expressions.

Related Links

Adding Java 8 support to Eclipse kepler

Background

In last post we saw how to use Lambda expressions (well at an introductory level). It is equally important to run it in an IDE like Eclipse to start with. Hence in this post we will see how to compile and run Java 8 compatible code in your Eclipse. Eclipse has a JRE which it uses to compile and run the code and there is compiler compliance level  which basically recognizes your syntax. For example something like below - 

List<String> myList = new ArrayList<>();

won't work with compliance level 6 as the diamond structure was introduced in Java 7. So compliance level 7 is required. We will come to all of this in a moment. This is just a background to get you started. My Eclipse version is Kepler (After upgrading from Juno)- 




NOTE : Eclipse Kepler SR2 & Eclipse Luna are the versions that will support Java 8. Older versions will not (And by not I mean you will see compiler warnings, you can still compile and run if you have Java8 JRE). So of you are using older versions please consider upgrading.

Getting Started

First lets take care of Java compiler compliance level. For that go to Help -> Install new Software



Next add following URL to it and press enter -
  • http://download.eclipse.org/eclipse/updates/4.3-P-builds/
Then select Eclipse java 8 support (for Kepler)



Click Next.Accept TnC and click Finish. Next select 1.8 as compiler compliance level.



You will also need to add JRE 8 as your java runtime (Windows -> Preference -> Installed JREs).



NOTE : Juna 4.4 SR2 already has that. So you can directly download it to save all this migration trouble.

Related Links

t> UA-39527780-1 back to top