4

I've just started working with Java 8 and I'm struggling with this code snippet:

paramsValues[idx++] = new ReplyMessage<JsonObject>() {
    @Override
    public void reply(JsonObject reply) {
        message.reply(reply);
    }
};

How to convert this to Lambda style?

1
  • Is there any online converter tool? Commented Jul 6, 2023 at 15:04

3 Answers 3

4

If ReplyMessage is a functional interface, you could do

paramsValues[idx++] = reply -> message.reply(reply);

Here's a full example with stub implementations of the other classes in your question:

// Stub classes
class JsonObject { }

@FunctionalInterface
interface ReplyMessage<T> {
    void reply(T jo);
}

class LambdaDemo {
    public static void main(String args[]) {

        // Dummy variables
        ReplyMessage<JsonObject> message = new ReplyMessage<JsonObject>() {
            public void reply(JsonObject jo) {}
        };
        ReplyMessage[] paramsValues = new ReplyMessage[5];
        int idx = 0;

        // Your code, lambdafied
        paramsValues[idx++] = reply -> message.reply(reply);

        // Or,
        // paramsValues[idx++] = message::reply;

        // But then you could perhaps do with just ...
        // paramsValues[idx++] = message;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Interface should have a generic type (interface ReplyMessage<T>)
@LouisWasserman, right. I just wanted to be a bit more verbose for educational purposes.
In my case 'paramsValues' is defined as follow: Object[] paramsValues = new Object[params.length]; so I get a compilation error: "The target type of this expression must be a functional interface"
Try this: paramsValues[idx++] = (ReplyMessage<?>) ((JsonObject reply) -> message.reply(reply));
Why not paramsValues[idx++] = (ReplyMessage<JsonObject>) reply -> message.reply(reply); as it’s only the target type that has to be included; there is no reason to make anything else more complicated compared to the original solution.
1

Lambda expressions are only possible with Functional Interfaces (Interfaces with only one method, such as Runnable or ActionEvent)

If ReplyMessage is a functional interface, you can do:

paramsValues[idx++] = reply -> message.reply(reply);

Lambda expressions are formed in this pattern: parameters that the method should take, then -> then the method body

Here is the code of how ReplyMessage interface should look like:

@FunctionalInterface
interface ReplyMessage<T> {
    void reply(T jo);
}

For more information, consider reading this.

1 Comment

The usage of @FunctionalInterface is optional. Also the restriction is not "only one method" but one abstract method. That's a rather big difference. I wonder what you mean by "take parameters". A function has parameters and takes arguments, if you meant that.
0
paramValues[idx++] = reply -> message.reply(reply);

Or

paramValues[idx++] = reply -> {
    return message.reply(reply);
}

It will work as long as ReplyMessage<JsonObject> is functional interface and paramValues is of type ReplyMessage<JsonObject>.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.