Tech

20141127

Java's parametrize Lambda;

Lambda is a new feature on the java se - JDK 8; yeat not implemented on the java EE specs. This now tool provides a lot more flexibility for the java programmers to code reuse, but also can generate unnecessary complexity;

All you really need is an interface with a single method. The annotation @FunctionalInterface (java.lang.annotation.FunctionalInterface - JDK 8), only enforces that the interface must have one and only one abstract method, but you could live without it.  To me, is just to tell others programmers not to declare anything else;

Example:
@FunctionalInterfaceinterface Calculator{    double calculate(int x , int y); }
or
interface Calculator{    double calculate(int x , int y); }
Both Works the same, and a consequence it limits other Object Oriented aspect as method overloading; 

Bad Example: 
interface Calculator{    double calculate(int x , int y);     double calculate(Long x , int y); }
Wrapping also seems not to have problems with Lambda expressions:

package lambda.expression;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//Lambda primitive type
@java.lang.FunctionalInterface
interface Calculator {
    double calculate(long x, int y);
}

//lambda generic Object
@java.lang.FunctionalInterface
interface ObjectImpl<E> {
    E doOString(E obj1, E obj2);
}

public class Test {
    public static void main(String[] argv) {
        //This is a very simple example; with declaration type and passing the expression;
        Calculator calc = (long x, int y) -> Math.min(x, y);
        System.out.printf("%6.2f\n", calc.calculate(255, 7));
       
        //A little more complex, is the use of collection;
        List<Calculator> listCalc = new ArrayList<>();
        listCalc.add((x, y) -> x + y);
        listCalc.add((x, y) -> x - y);
        listCalc.add((x, y) -> x / y);
        listCalc.add((x, y) -> x % y);
        listCalc.add((x, y) -> Math.hypot(x, y));
        Iterator<Calculator> interate = listCalc.iterator();
        while (interate.hasNext()) {
            Calculator calculator = interate.next();
            System.out.printf("%6.2f\n", calculator.calculate(255, 7));
        }
        //to make your day, Generics is here to add some more complexity;
        ObjectImpl<String> objectImpls = (obj1, obj2) -> obj1 + obj2;
        String result = objectImpls.doOString("Hello ", "World!!");
        System.out.println(result);
        //Adding with Integer Object and wrappers;
        ObjectImpl<Integer> objectImplsNum = (obj1, obj2) -> obj1 + obj2;
        Integer resultint = objectImplsNum.doOString(new Integer(2),3);
        System.out.println(resultint);
       
    }
}

The results: 

run:
  7.00
262.00
248.00
 36.00
  3.00
255.10
Hello World!!
5
BUILD SUCCESSFUL (total time: 2 seconds)

Lambda like break, continue, label and other features, can turn your code more complex; but its here to stay, and we should understand how it works and use it with precaution;  



No comments:

Post a Comment