Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generic Types and Entry

Tags:

java

generics

I am trying to implement a generic DDLinkedList of Entries. I have the following classes defined.

class DoublyLinkedList<T extends Comparable<T>> 
class DLLNode<T extends Comparable<T>> 
Entry<K extends Comparable<K>, V> implements Comparable<Entry<K, V>>

Once I try to create an array of DoublyLinkedlist of type Entry as below:

DoublyLinkedList<DLLNode<Entry<K, V>>> array[] = (DoublyLinkedList<DLLNode<Entry<K, V>>>[]) new DoublyLinkedList[TABLE_SIZE];

I get an error message:

"The type DLLNode<Entry<K,V>> is not a valid substitute for the bounded
parameter <T extends Comparable<T>> of the type DoublyLinkedList<T>"

From my other standing I thought I could make a Generic Type T of Entry.

My question: Am I going about the wrong way of doing this or am I implementing it wrong?

like image 503
user2390775 Avatar asked Aug 31 '26 10:08

user2390775


1 Answers

Your definition for DoublyLinkedList says that it must take a type which is comparable with itself. But your definition for DLLNode does not implement Comparable<DLLNode<T>> which means that DLLNode is not comparable with its own type. So you can't use DLLNode as a parameter type in DoublyLinkedList.

like image 118
Bobulous Avatar answered Sep 02 '26 03:09

Bobulous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!