I do have troubles manipulating hashmaps. My problem is quite simple, but I cannot make it work…
I have an interface named MultilineWritable. My classes that do need to be marshalled on flat files do implement this interface, which define a String toFlatFormat() function and its opposite init(String flatFormat).
Now imagine that the Contract class do implement MultilineWritable and is identified by integers while the Person class do also implement this interface but is identified by Strings.
My function to marshall the HashMaps of contracts and persons looks like :
public void marshall(HashMap<Object, MultilineWritable>){}
My problem is I cannot find a way to cast a HashMap<String, Person> to a HashMap<Object, MultilineWritable>, even if String extends Object and Person extends MultilineWritable. Same for the casting of a HashMap<Integer, Contract>… consequently I cannot call my generic function.
Thx for any help regarding this problem.
Assuming your
marshallmethod only ever reads values, you should just change the declaration to:The problem is that if the existing method call were allowed, it could try to put any
MultilineWritableinto the map – even if it was the wrong map type:EDIT: For more details about wildcards, see the Java Generics FAQ.