I have two generic methods that calculate the sum of elements of a List. The signatures of the methods are
double method1(List<? extends Number> list)
- Here I am using a wildcard.<U extends Number> double sumOfList1(List<U> list)
- Here there is a name for type parameter.
Is there any difference between these two or they're same in terms of functionality ? Using a type parameter name instead of wildcard has any advantages ?
public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
public static <U extends Number> double sumOfList1(List<U> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
Thank you.