Nah Prime
Nah Prime

Reputation: 33

java.lang.Comparable<T> cannot be converted to T"

public class DoubleLinkedList<T> implements DoubleLinkedListADT<T> {
    //Double linked list node class
    public class DoubleLinkedListNode<T>  {
        T info;
        DoubleLinkedListNode<T> next;
        DoubleLinkedListNode<T> back;

        public DoubleLinkedListNode() {
            info = null;
            next = null;
            back = null;
        }

        public String toString() {
            return info.toString();
        }
    }

    protected int count;                     //number of nodes
    protected DoubleLinkedListNode<T> first; //reference to first node
    protected DoubleLinkedListNode<T> last;  //reference to last node

    public boolean equals(Object o) {
        if(o instanceof DoubleLinkedList) {
            DoubleLinkedList<T> d = (DoubleLinkedList) o;
            if(count != d.count){
              return false;
            }
            else{
                DoubleLinkedListNode<T> curr = first;
                DoubleLinkedListNode<T> curr2 = d.first;
                while(curr != null && curr2 != null){
                    Comparable<T> temp = (Comparable<T>) curr.info;
                    Comparable<T> temp2 = (Comparable<T>) curr2.info;
                    if(temp.compareTo(temp2) >= 0){
                      return false;
                    }
                    curr = curr.next;
                    curr2 = curr2.next;
                }
                return true;
            }
        }
        else
            return false;
    }
}    

the problem is on this if statement, "if(temp.compareTo(temp2) >= 0)".

The error is saying "incompatible types: java.lang.Comparable cannot be converted to T".

I think is the problem is when I am typecasting the object

Upvotes: 1

Views: 2973

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86764

First you must alter the declaration of DoubleLinkedListto

class DoubleLinkedList<T extends Comparable<T>>

so the compiler knows that T is a Comparable<T> and can ensure that whatever parameter class you provide will be guaranteed to have a compareTo() method.

Then, you don't need the temporaries, since T implements Comparable<T> you just compare the T instances directly:

while(curr != null && curr2 != null){
    if(curr.info.compareTo(curr2.info) >= 0){
      return false;
    }
    curr = curr.next;
    curr2 = curr2.next;
}

Not sure why you thought you needed to cast to Comparable<T>. Note that the definition of Comparable<T>.compareTo is

int compareTo​(T o);

and not

int compareTo​(Comparable<T> o)

You commented:

I added the extends Comparable but it was conflicting with my interface, and i didnt want to change my interface because this was for an assignment.

You have a conflict alright, but it's between the definition of the interface and the requirements of the assignment. You have added behavior into the list's equals() method that depends on an ordering comparison between contained elements. If this is truly part of the required behavior, then the interface type parameter T MUST be declared <T implements Comparable<T>> to enforce type safety. Consider what will happen when you instantiate a DoubleLinkedList where an ordering comparison is not meaningful for objects of the type parameter, for example

DoubleLInkedList<Map<String,Integer>> list = new DoubleLinkedList<>();

If you can implement the desired behavior in terms solely of equals() then you'd be OK omitting the type constraint. Otherwise you are giving up the type safety that generics were designed to provide, and the design is broken.

Upvotes: 1

Related Questions