java 7 lambda,转换Java 8 Lambda表达式以在Java 7中工作

这篇博客介绍了如何将使用Java 8 Lambda表达式的代码转换为Java 7兼容的形式。具体例子涉及将Predicate接口手动实现,以便在不支持Lambda的Java 7环境中运行。内容详细解释了如何创建一个匿名内部类来替代Lambda,并展示了如何在该类中实现等效的逻辑。
摘要由CSDN通过智能技术生成

Trying to convert the this section of code to get rid of the lambda expressions so it'll be able to work in java 7

System.out.println(nicksGraph.findPath(nicksGraph.nodeWith(new Coordinate(0,0)), (a->a.getX()==3 && a.getY()==1), new PriorityQueue, Integer>(funcTotal)));

I've looked around but maybe I'm just not understanding it correctly.

解决方案

In Java 8, a lambda expression is a substitute for an anonymous inner class that implements a functional interface. It looks like here you're using a Predicate, because the expression is a boolean.

The Predicate interface was introduced in Java 8, so you need to re-create it yourself. You will not be able to create default or static methods, so just keep the functional interface method.

public interface Predicate

{

public boolean test(T t);

}

Then, replace the lambda expression with the anonymous class declaration.

System.out.println(nicksGraph.findPath(

nicksGraph.nodeWith(new Coordinate(0,0)),

// Replacement anonymous inner class here.

new Predicate() {

@Override

public boolean test(Coordinate a) {

// return the lambda expression here.

return a.getX()==3 && a.getY()==1;

}

},

new PriorityQueue, Integer>(funcTotal)));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值