Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

help with generics - extending hashmap

807605Jul 11 2007 — edited Jul 12 2007
I'm trying to extend the HashMap class to accept multiple values per key. I'd like .put(key,value) to put a single key/value pair. I'd like .get(key) to return a Vector<V> of all the values associated with key, and .get(key, index) to return a single value of type V for that key. I am using Eclipse 3.2 and Java 1.5.

The problem I am having is my .put(K key,V value) method won't compile, I get the error:

Name clash: The method put(K,V) of type OneToManyHashMap<K,V> has the same erasure as put(K,V> of type HashMap<K,V> but does not override it

My code (roughly):
public class OneToManyHashMap<K,V> extends HashMap<K, Vector<V>> {
    public V get(Object key, int index) { return get(key).get(index); } 
    public V put(K key, V value) { ... }
    public V remove(Object key, int index) { return get(key)
}
Yes, I know there is an extra > in the code. It's wasn't there when I typed the message and it's not there when I try and edit the message to remove it :(

Message was edited by:
jiggersplat
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2007
Added on Jul 11 2007
14 comments
1,476 views