如何将Java8 Lambda表达式转换为相应的Java7代码

时间:2015-11-07 13:47:20

标签: java lambda java-8

我一直在想如何将下面的lambda表达式(这是Java8最大的语言特性)转换为相应的Java 7

final JmsBolt jmsBolt = new JmsBolt();
jmsBolt.setJmsMessageProducer((session, input) -> {
        final String json = "{\"stateCode\":\"" + input.getString(0) + "\", \"sentiment\":" + input.getInteger(1) + "}";
        return session.createTextMessage(json);

这是JmsBolt类:

public class JmsBolt extends BaseRichBolt {

 ...
 public void setJmsMessageProducer(JmsMessageProducer producer){
    this.producer = producer;
 } 
 ...
}

这是JmsMessageProducer功能界面:

public interface JmsMessageProducer extends Serializable{

public Message toMessage(Session session, Tuple input) throws JMSException;
}

1 个答案:

答案 0 :(得分:2)

我认为匿名课程可以帮助你:

jmsBolt.setJmsMessageProducer(new JmsMessageProducer() {

    @Override
    public Message toMessage(Session session, Tuple input) {
        ...
    }

});