java - Issue with declaration of Map<String,Class<? extends Serializable>> -
java provides me <? extends class> way of filtering java classes can use build in case new hashmap, example:
i can that:
map<string,? extends serializable> map1 = new hashmap<string,string>(); it correct, because string implements serializable, compiler let me that.
but when try it:
map<string,genericclass<? extends serializable>> map2 = new hashmap<string, genericclass<string>>(); being genericclass it:
public class genericclass<t> { . . . } the compiler throw error saying:
type mismatch: cannot convert hashmap<string,genericclass<string>> map<string,genericclass<? extends serializable>> i know, happen?
maybe compiler cannot detect extends class being part of generic type.
you need use following:
map<string, ? extends genericclass<? extends serializable>> map2 = new hashmap<string, genericclass<string>>(); nested wildcards different top-level wildcards - latter perform wildcard capture. result, hashmap<string, genericclass<string>> considered inconvertible map<string, genericclass<? extends serializable>>, because genericclass<? extends serializable> concrete type argument (and because generics aren't covariant).
see post further information on nested wildcards: multiple wildcards on generic methods makes java compiler (and me!) confused
Comments
Post a Comment