I want to create a generic function that can nearly always find the largest item in any array. Based on this https://stackoverflow.com/questions/25779184/explanation-of-generic-t-extends-comparable-super-t-in-collection-sort-com post, I think the following will work in most cases:
// Comparable<? super T>> should mean that the type either implements comparable to itself, its supertype, or its supertype implements comparable to itself, not sure
static <T extends Comparable<? super T>> T maxOf(List<T> list) {
if (list.isEmpty()) // Ensure list is not empty - any other checks I need?
throw new IllegalArgumentException("Arguments must not be empty.");
T max = list.get(0); // set the max value to the first value
for (T element : list) { // For each element in the list
if (max.compareTo(element) < 0) // check if the current max is lower
max = element; // set max to the new highest element
}
return max;
}
is there anything I'm missing, need to account for, or need to change in order to make this as universal as possible, for any values that can be compared with eachother?