Function abstraction:
public abstract class Function<X, Y> {
abstract Y apply(X x);
}
max method implementation
public static <V extends Comparable<V>> Function<List<V>, V> max() {
return new Function<List<V>, V>() {
@Override
public V apply(List<V> list) {
return Collections.max(list);
}
};
}
And usage (how it should look like)
Date result = max().apply(datesList);
But I get this error and don’t understand why it requires Object
incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) V
found : <V>project.Function<java.util.List<V>,V>
required: java.lang.Object
I have read big amount of similar QA but didn’t get how to fix this.
Thanks.
Java has a very limited type inference. If you write this:
it is not sophisticated enough to infer the type parameter of the
max()method,V, so it takesjava.lang.Objectinstead. You could try this:Or, if you want to write it in one line, you could do the following, assuming that your
max()method is defined in a class namedExample: