Skip to Main Content

Java APIs

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!

Casting a HashMap

JörgNov 27 2006 — edited Nov 28 2006
Hello,

I feel a dilemma with the following code: generics are of no use to ObjectInputStream.readObject() ...
static HashMap<Integer,Character> map= new HashMap<Integer,Character>();
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream
					(new FileInputStream("myFile")));
map = (HashMap)ois.readObject(); // [unchecked] unchecked conversion
map.put(new Integer(nr), new Character(c));
... but after removing the generics from the HashMap definition, now HashMap.put(...) asks for them.
static HashMap map= new HashMap();
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream
					(new FileInputStream("myFile")));
map = (HashMap)ois.readObject();
map.put(new Integer(nr), new Character(c)); // [unchecked] unchecked call to put(K,V)
A cast like
map = (HashMap<Integer,Character>)ois.readObject();
is not possible (G. Bracha's Tutorial, 7.2).
Is there still a way to satisfy the compiler?

Greetings
J�rg
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2006
Added on Nov 27 2006
3 comments
143 views