I have to write a method to sort both Integers and Doubles.
public ArrayList<Number> sortDescending(ArrayList<Number> al){
Comparator<Number> c=Collections.reverseOrder();
Collections.sort(al,c);
return al;
}
public ArrayList<Number> sortAscending(ArrayList<Number> al){
Collections.sort(al);
return al;
}
The problem is that in sortAscending, the following error occurs:
Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (ArrayList). The inferred type Number is not a valid substitute for the bounded parameter < T extends Comparable < ? super T>>
You need to use a generic upper bound of
Numberintersecting withComparable<T>:All JDK
Numbers(egFloat,Integeretc) match this typing.For the uninitiated, the syntax
<T extends A & B>is the way you boundTto bothAandB.FYI, there is no syntax for “or” logic (nor would it make sense if you think about it)