Generic class using generic parameter

generics, java

Solution

you can change your tree to :

class Tree<K, T, V> {
    public void insert(Node<K, T, V> node) {
        K key = node.getKey();
        // do something...
    }
    // ... etc...
}

or if you want to bind it to Node,

class Tree2<N extends Node<K, T, V>, K, T, V> {
    public void insert(N node) {
        K key = node.getKey();
        // do something...
    }
    // ... etc...
}

Problem

I want to achieve the following. I have a generic class `Node<K,T,V>` which looks as follows: ``` public class Node<K,T,V>{ /** * @return the key */ public K getKey() { return key; } /** * @param key the key to set */ public void setKey(K key) { this.key = key; } // etc... ``` Now I want to have a class `Tree` which operates on Nodes with arbitrary parametrized types: ``` public class Tree <Node<K,V,T>> { public void insert(Node<K,V,T> node){ K key = node.getKey(); // do something... } // ... etc... } ``` This, however, does not work, since Eclipse tells me the line `public class Tree <Node<K,V,T>>` does not look good :) If I change it to ``` public class Tree <Node> { ``` It tells me that the type Node is hiding the type Node. How can I achieve that from the Tree class I can properly access the types K, V and T? I'm pretty sure this question has been answered a bazillion times. I did, however, not find anything at all - so sorry for that!!! Cheers.

Original source