Strange compile error in java -
this question has answer here:
i'm trying pass variable of type:
hashmap<foo, hashset<bar>>
to method, expects:
map<foo, set<bar>>
i think should work, i'm getting following compile error:
java: method setmenu in class com.xx.client.layout.layout cannot applied given types; required: java.util.map<com.xx.shared.model.usertype,java.util.set<com.xx.shared.dto.model.menuitemdto>> found: java.util.hashmap<com.xx.shared.model.usertype,java.util.hashset<com.xx.shared.dto.model.menuitemdto>> reason: actual argument java.util.hashmap<com.xx.shared.model.usertype,java.util.hashset<com.xx.shared.dto.model.menuitemdto>> cannot converted java.util.map<com.xx.shared.model.usertype,java.util.set<com.xx.shared.dto.model.menuitemdto>> method invocation conversion
to attempt convince you, given
hashmap<foo, hashset<bar>> mymap;
you expect do
hashset<bar> ahashset = mymap.get(afoo);
but if had
public void somemethod(map<foo, set<bar>> amapparameter) {...}
and expected
somemethod(mymap);
to work, somemethod
do
public void somemethod(map<foo, set<bar>> amapparameter) { amapparameter.put(afoo, new treeset<>()); }
your original
hashset<bar> ahashset = mymap.get(afoo);
would fail classcastexception
.
compile time type safety has guarantee doesn't happen. therefore compiler doesn't allow method call.
related:
Comments
Post a Comment