Map<V, Set><V>> not a supertype of HashMap<V, HashSet><V>> ?
Hi,
I am relatively new to generics, so maybe my questions is dumb, if so please bear with me ... I have asked colleagues, but we're clueless.
I have the following code:
publicabstractclass myAbstractClass<T>{
publicabstract Set<T> getNewSet();
publicabstract Map<T, Set><T>> getNewMap();
}
publicclass myClass<T>extends myAbstractClass<T>{
public Set<T> getNewSet(){
returnnew HashSet<T>();
}
public Map<T, Set><T>> getNewMap(){
returnnew HashMap(T, HashSet<T>);
}
}
Everything works fine forgetNewSet(), but forgetNewMap() the compiler gives the error:Type mismatch: cannot convert from HashMap<V,HashSet><V>> to Map<V,Set>. I don't understand why...
The only solution I could find, thanks to a codeveloper's advice is:
publicclass myClass<T>extends myAbstractClass<T>{
public Set<T> getNewSet(){
returnnew HashSet<T>();
}
public Map<T, Set><T>> getNewMap(){
returnnew HashMap(T, Set<T>);
}
}
Furthermore, if I modify my second class as follows:
publicclass myClass<T>extends myAbstractClass<T>{
public Set<T> getNewSet(){
returnnew HashSet<T>();
}
public Map<T, Set><T>> getNewMap(){
returnnew HashMap(T, HashSet<T>);
}
public Map<T, Set><T>> getAnotherNewMap(){
returnnew HashMap(T, HashSet<T>);
}
}
Then I get the same compiler error forgetAnotherNewMap()... I'm definitely lost.

