Search code examples
javainterfaceprimitive-types

LinkedList of type that implements the comparable interface


I'm looking for how to pass as a parameter of a method a linkedList of a type which implements the Comparable interface.

 public static void mergeSort(LinkedList<Comparable> list){}

I tried that but when I pass a primitive type such as Integer as a parameter it doesn't work. If someone have an idea ?


Solution

  • Simply let the compiler know that the input type T extends Comparable<T>.

    public static <T extends Comparable<T>> void mergeSort(final LinkedList<T> list)
    

    I would not use the ?, which is a wildcard and represents an unknown type.
    Explicitly specifying the generic type is always better (see this question).