So I have this piece of code which uses several maps to store some simple classes. Now I also want to look op some of the values based on for example the x position of this item. I tried to do something with Generics and lambda's, but it just didn't work out. Also if there is an easier way to do this, please tell. I have most of my experience in Python, so this might just not be practical in Java.
interface Compare {
public <T, P> boolean apply(T obj, P comp);
}
class Utils {
public static <T, P> List<T> retrieve(Collection<T> args, P value, Compare c) {
List<T> r = new ArrayList<T>();
for (T i: args) {
if (c.apply(i, value)) {
r.add(i);
}
}
return r;
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
This doesn't raise any errors as of yet, but when I try to actually use the retrieve function on a HashMap it doesn't work.
Point p = Utils.retrieve(points.values(), 0, (Point p, Integer x) -> {return p.x == x;});
There are two errors in eclipse:
- At Utils.retrieve, eclipse notes that: The method retrieve(Collection, P, Compare) in the type Utils is not applicable for the arguments (Collection, int, (Point p, Integer x) -> {})
- At the lambda expression, eclipse notes that: Illegal lambda expression: Method apply of type Compare is
How would one handle this situation? My thanks in advance.
interface Compare<T, P> { boolean apply(T obj, P comp); }, with the generics on the interface, not the method.