Convert java 8 lambda expressions to work in java 7

Trying to convert this section of code to get rid of lambda expressions so that it can 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 looked around, but maybe I just don't get it right.

+3
source share
3 answers

In Java 8, the lambda expression is a replacement for an anonymous inner class that implements a functional interface. It looks like you use Predicatebecause expression boolean.

PredicateInterface was introduced in Java 8 , so you need to recreate it yourself. You cannot create methods defaultor static, therefore, just use the functional interface method.

public interface Predicate<T>
{
    public boolean test(T t);
}

- .

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)));
+7

findPath Predicate .

 new Predicate<>(){
     public boolean test(Integer i){
          return a.getX() == 3 && a.getY() == 1;
     }
 }

Predicate, java8. java7, , lambdas .

+2

Retrolambda. backport Java - Java 7, 6 5.

, Retroweaver et al. Java 5 generics Java 1.4, Retrolambda Java 8 , try-with-resources Java 7, 6 5. , Java 8, -, Java

0

Source: https://habr.com/ru/post/1609319/


All Articles