Lambda Expression Examples

Original code to loop through the List<Element> elements:

for(Element e: elements){
// do something with e
}

Use of -> (in case there is no need to declare the type):

elements.forEach(e -> {
// do something with e
});

Use of :: (in case you already have a method to deal, and only deal with e):

elements.forEach(instance::method);

Note: The instance can be a reference, a new statement, or this keyword. If the method is static, the first part of :: can also be the class name.

In some other cases, there might be no parameter, or more than one parameters.

If the interface of the call parameter only needs one parameter:

void call(new Interface(){
    void execute(Object o){ // only single-method interfaces can be translated to Lambda Expression
        // some code
    }
});

It can be translated to:

call(e -> { // e represents Object o on above code stub
    // some code
    // since the return type of Interface().execute() method is void, there's no need of return statement
});

Only one line statement:

call(e -> statement);

Using method delegation:

call(instance::method);
// void method(Object o){ // if Interface().execute() demands a return, then the return type of this method must be same
//     // some code
// }

If the interface of the call parameter needs no parameter:

void call(new Interface(){
    void execute(){ // only single-method interfaces can be translated to Lambda Expression
        // some code
    }
});

It can be translated to:

call(() -> { // () is used to indicate there's no parameter
    // some code
    // since the return type of Interface().execute() method is void, there's no need of return statement
});

Only one line statement:

call(() -> statement);

Using method delegation:

call(instance::method);
// void method(){ // if Interface().execute() demands a return, then the return type of this method must be same
//     // some code
// }

If the interface of the call parameter only needs two or more parameters:

void call(new Interface(){
    void execute(Object o1, Object o2){ // only single-method interfaces can be translated to Lambda Expression
        // some code
    }
});

It can be translated to:

call((e1, e2) -> { // (e1, e2) represents o1 and o2 on above code stub
    // some code
    // since the return type of Interface().execute() method is void, there's no need of return statement
});

Only one line statement:

call((e1, e2) -> statement);

Using method delegation:

call(instance::method);
// void method(Object o1, Object o2){ // if Interface().execute() demands a return, then the return type of this method must be same
//     // some code
// }

Function interface

The only way to use lambda express without a method call is to use functional interface (with @FunctionalInterface annotation). E.g. Function interface.

@FunctionalInterface
public interface Function<T, R> {...}  // T: input type R: return/result type

An example of using Functional interface:

Function<Long, Long> square = x -> x * x;

or

Function<Long, Long> square = instance::method;
// Long method(Long x){return x*x;}

Wrong:

NoneFunctionalClass<Long, Long> square = instance::method; // can not pass compilation
Advertisements