如何将此代码转换为java8 lambda [英] How to convert this code to java8 lambda
本文介绍了如何将此代码转换为java8 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始使用Java 8而且我正在努力使用这段代码:
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);
}
};
如何将其转换为Lambda风格?
How to convert this to Lambda style?
推荐答案
如果 ReplyMessage
是功能界面,你可以做
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;
}
}
这篇关于如何将此代码转换为java8 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文