Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve `Raw use of parameterized class 'Comparable'` warning?

Got to implement the method below for an assignment which it's subject is "WildCards", but don't know where to use wildcards in order to resolve the warning.

static <T extends Comparable> T findMax(T ... items)
{
    T max = items[0];
    for (T item : items)
        if (item.compareTo(max) > 0)
            max = item;
    return max;
}

Any ideas ?

like image 943
Parsa Noori Avatar asked May 31 '20 21:05

Parsa Noori


1 Answers

Comparable is a generic interface, so to use it safely you must always specify the generic type to use. In your case, something like:

<T extends Comparable<T>>

is likely what you're looking for. Otherwise, the compiler is unable to help you verify that the types are actually compatible in all scenarii.

like image 101
Dici Avatar answered Oct 11 '22 19:10

Dici