hashmap serialization
I though hashmap in JDK 1.5 or 1.6 ahs built-in method of serializtion for hashmap.
after google on hashmap serialization, I did not find any easy way to deserilize my own hashmap declared with
HashMap<string, myObjCls>
to use my own objectinputstream, I will get unchecked warning
yet I can't find out info to use the builtin serilaztion and serialization when given the file spec.
I am hoping I don't have to build my own custom class.
BTW myObjCls is nothing but a defenerate class with some attributes public in the scope of the package I am building.
[606 byte] By [
g_gunna] at [2007-11-27 4:50:36]

Demo:import java.io.*;
import java.util.*;
public class X implements Serializable {
private String s;
public X(String s) {this.s = s;}
public String toString() {return s;}
public static void main(String[] args) throws Exception {
Map < String, X > m = new HashMap < String, X >();
m.put("one", new X("uno"));
m.put("two", new X("dos"));
m.put("three", new X("tres"));
File f = new File("temp.ser");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(m);
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
@SuppressWarnings("unchecked")
Map < String, X > m2 = (Map < String, X >) in.readObject();
in.close();
System.out.println(m2);
}
}
You don't *have* to use it, if you don't mind the warning, but no, there is nobetter way, if you want to end up with a parameterized type like Map < String, X > .Just accept it.