Is there a way to make this method appropriately general and get rid of warnings?
public static <T> void sortByValue(List<T> list, final boolean ascending, @SuppressWarnings("rawtypes") final Function<? extends Comparable, T> valueFunction) { Collections.sort(list, new Comparator<T>() { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public int compare(T o1, T o2) { final Comparable v1 = valueFunction.call(o1); final Comparable v2 = valueFunction.call(o2); return v1.compareTo(v2) * (ascending ? 1 : -1); } }); }
I tried Function<? extends Comparable<?>, T> Function<? extends Comparable<?>, T> and Function<? extends Comparable<? extends Comparable>, T> Function<? extends Comparable<? extends Comparable>, T> Function<? extends Comparable<? extends Comparable>, T> , but did not compile with an error when compareTo called. For the first:
The compareTo method (capture # 9-of?) In the Comparable type is not applicable for arguments (capture # 10-of? Extends Comparable)
java generics unchecked
Bart van Heukelom Dec 07 '11 at 12:53 on 2011-12-07 12:53
source share