The problem is that you're mixing what's being compared. You're trying to compare the LinearNode
object (which holds an E
) to an actual E
. LinearNode<E>
shouldn't implement Comparable<E>
; if anything, it might implement Comparable<LinearNode<E>>
, and the type parameter should probably be E extends Comparable<E>
.
If you want to order LinearNode
s based on the ordering of their underlying elements, you should use something like this:
// in LinearNode
public int compareTo(LinearNode<E> otherNode) {
return this.element.compareTo(otherNode.element);
}
(Note that the Java sorted collections don't require elements to implement Comparable
, since you can provide a custom Comparator
for any of them, but in the case of this assignment it's probably fine to require that E extends Comparable<E>
.)
(Note 2: If you're using Generics, any cast, such as your (Comparable<E>)
, is a red flag; the purpose of the Generics system is to eliminate the need for most explicit casts.)