1

I'm trying to create a method that allows me to make use of what I believe is called lambdas, to execute a method over a series of connections.

Here's my code that I've come up with after some research, but it doesn't work:

performGlobalAction(()->{
    // doSomething();
});

You'll also need to see the method I would assume:

private <T> void performGlobalAction(Callable<T> action) {
    for(int i = 0; i < connectionList.size(); i++) {
        connectionList.get(i).performAction(action);
    }
}

This provides the following error:

The method performAction(Callable<T>) in the type Connection is not
applicable for the arguments (() -> {})

The goal of this method is to allow myself to construct a method "on the go" without creating a void for it.

Is this possible? It seems like I've used plenty of statements that have done this before. It seems like this is actually exactly how lambdas statements work.

3
  • Oh, sorry, updating. Commented Nov 18, 2014 at 2:14
  • Much better. Now the misunderstanding is on how lambdas map to instances of functional interfaces. Your Callable type, assuming it's java.util.concurrent.Callable<T>, has a method with a return type of T. The body of your lambda expression does not have an expression or statement that can act as a return value. Commented Nov 18, 2014 at 2:17
  • @SotiriosDelimanolis - Not sure that I completely understand all of this, going off of the information found here: oracle.com/technetwork/articles/java/lambda-1984522.html -- I can't quite figure it out, how would I use lambda in the context that I'm attempting? Is it possible? Commented Nov 18, 2014 at 2:23

1 Answer 1

5

The call method of the Callable interface returns a value of type T. Your lambda is simply shorthand for the call method, and likewise should return a T value.

Any interface that meets the requirements of a FunctionalInterface can be substituted by a lambda expression. Such an interface will have a single abstract method, one with no default implementation. For your question, the interface is Callable, and the abstract method is call. The lambda expression then acts as the body of that abstract method in an anonymous implementation of that interface.

Let's take as an example a method doStuff(Callable<Integer> stuff). To satisfy this interface, you could give an anonymous class:

doStuff(new Callable<Integer>(){
    public Integer call(){
        return 5;
    }
});

Or you could use a lambda:

doStuff( () -> {
    return 5;
} );

Or even more succinctly:

doStuff( () -> 5 );

If your method doesn't have a return type, perhaps Runnable would be a better fit.

See also: Lambda Expressions (Oracle) - 'Use Standard Functional Interfaces with Lambda Expressions'

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the link on Functional interfaces, helped with my understanding of lambda quite a bit, but I'm still having a hard time figure out this implementation. I know it's asking a little much, but could you perhaps provide a psuedo / working example?
The answer doesn't even have to be a lambda implementation, something similar to the C# delegate would suffice as-well, just something to pass a (on the fly)method over a parameter.
My syntax may be off, FYI, but don't have time to correct at the moment. Edit away, folks!
A Runnable doesn't perform any sort of threading; it is, after all, only an interface. When called, it will run in the current thread.
"Any interface annotated as a FunctionalInterface can be substituted by a lambda expression" - Well, it's even better: FunctionalInterface is just an informative annotation. You can also use lambdas with plain interfaces, as long as they comply the other requirements you mentioned.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.