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.
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<T>
{
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<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)));
findPath requires a Predicate as second argument. You could replace the lambda with
new Predicate<>(){
public boolean test(Integer i){
return a.getX() == 3 && a.getY() == 1;
}
}
But the main problem will simply be the availability of Predicate
itself, since it's a java8 feature. this code won't run in java7, no matter if you use lambdas or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With