Trouble swapping values as keys in generic java BST class

Posted by user1729869 on Stack Overflow See other posts from Stack Overflow or by user1729869
Published on 2012-11-18T17:34:49Z Indexed on 2012/11/18 23:01 UTC
Read the original article Hit count: 192

Filed under:
|

I was given a generic binary search tree class with the following declaration:

public class BST<K extends Comparable<K>, V>

I was asked to write a method that reverses the BST such that the values become the keys and keys become values. When I call the following method (defined in the class given)

reverseDict.put(originalDict.get(key), key); 

I get the following two error messages from Netbeans:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: BST.put

And also:

no suitable method found for put(V,K) method BST.put(BST<K,V>.Node,K,V) is not applicable (actual and formal argument lists differ in length) method BST.put(K,V) is not applicable (actual argument V cannot be converted to K by method invocation conversion) where V,K are type-variables:

V extends Object declared in method <K,V>reverseBST(BST<K,V>)

K extends Comparable<K> declared in method <K,V>reverseBST(BST<K,V>)

From what the error messages are telling me, since my values do not extend Comparable I am unable to use them as keys. If I am right, how can I get around that without changing the class given (maybe a cast)?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics