The method compareTo(Comparable) is undefined for the type Comparable



So i am building a library as project that covers not all but most of the data structures and i found myself with this problem:

package structure.tree;

import structure.Structure;
import structure.node.BinaryNodeKeyValue;

public class AVLTree<Comparable,V> extends Tree<BinaryNodeKeyValue<Comparable,V>> {
    
    private static final long serialVersionUID = 5046115177325966348L;

    public AVLTree(){

    }

    @Override
    public int compareTo(Structure<BinaryNodeKeyValue<Comparable,V>> o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean containsValue(BinaryNodeKeyValue<Comparable,V> elem) {
        return containsValueAux(((BinaryNodeKeyValue<Comparable,V>) super.getValue()), elem);
    }

    private boolean containsValueAux(BinaryNodeKeyValue<Comparable,V> root, BinaryNodeKeyValue<Comparable,V> elem){
        if(root == null) return false;
        else {
            if(root.equals(elem)) return true;
            else return containsValueAux(root.getLeft(), elem) || containsValueAux(root.getRight(), elem);
        }
    }

    public boolean containsKey(Comparable key){
        return containsKeyAux(((BinaryNodeKeyValue<Comparable,V>) super.getValue()), key);
    }

    private boolean containsKeyAux(BinaryNodeKeyValue<Comparable,V> root, Comparable key){
        if(root == null) return false;
        else {
            if(root.getKey().compareTo(key) > 0) return containsKeyAux(root.getRight(), key);
            else if(root.getKey().compareTo(key) < 0) return containsKeyAux(root.getLeft(), key);
            else return true;
        }
    }

    @Override
    public void deleteValue(BinaryNodeKeyValue<Comparable,V> elem) {
        // TODO Auto-generated method stub

    }
    
    @Override
    public void insertValue(BinaryNodeKeyValue<Comparable,V> elem) {
        // TODO Auto-generated method stub

    }

    @Override
    public BinaryNodeKeyValue<Comparable,V>[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    public BinaryNodeKeyValue<Comparable,V> get(BinaryNodeKeyValue<Comparable,V> root, Comparable key){
        return getAux(root, key);
    }

    private BinaryNodeKeyValue<Comparable, V> getAux(BinaryNodeKeyValue<Comparable, V> root, Comparable key) {
        return null;
    }

}

At rows 40 and 41 (rows 3 and 4 of method containsKeyAux) it says “The method compareTo(Comparable) is undefined for the type Comparable” and this blows my mind cause the method compareTo is actually defined inside Comparable interface only. VS Code is also showing me a warning at row 6 that says “The type parameter Comparable is hiding the type Comparable” but i am trying to make the Comparable type as generic as possible cause the key of nodes could be a String, Integer, or a different type of Object.

Answer

When you declare a generic like this AVLTree<Comparable,V> you have created a class with two generic types Comparable and V and Comparable has nothing to do with the interface Comparable, they just happen to have the same name.

You probably meant

class AVLTree<T extends Comparable, V>


Source: stackoverflow