转换Java 8 Lambda表达式以在Java 7中工作 [英] Converting java 8 lambda expression to work in java 7

查看:64
本文介绍了转换Java 8 Lambda表达式以在Java 7中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试转换此部分代码以摆脱lambda表达式,以便能够在Java 7中工作

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<Node<Coordinate>, Integer>(funcTotal)));

我环顾四周,但也许我只是不正确地理解它.

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

推荐答案

在Java 8中,lambda表达式替代了实现功能接口的匿名内部类.看起来您在这里使用的是Predicate,因为表达式是boolean.

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.

Predicate接口是Java 8中引入的,因此您需要自己重新创建它.您将无法创建defaultstatic方法,因此只需保留功能接口方法即可.

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<T>
{
    public boolean test(T t);
}

然后,将lambda表达式替换为匿名类声明.

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<Coordinate>() {
       @Override
       public boolean test(Coordinate a) {
          // return the lambda expression here.
          return a.getX()==3 && a.getY()==1;
       }
    },
    new PriorityQueue<Node<Coordinate>, Integer>(funcTotal)));

这篇关于转换Java 8 Lambda表达式以在Java 7中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆