Complex HashMap has different hashCode after serialization
Posted
by woezelmann
on Stack Overflow
See other posts from Stack Overflow
or by woezelmann
Published on 2010-03-19T11:16:20Z
Indexed on
2010/03/19
11:21 UTC
Read the original article
Hit count: 391
I am parsing a xml file into a complex HashMap looking like this:
Map<String, Map<String, EcmObject>
EcmObject:
public class EcmObject implements Comparable, Serializable {
private final EcmObjectType type;
private final String name;
private final List<EcmField> fields;
private final boolean pages;
// getter, equals, hashCode
}
EcmObjectType:
public enum EcmObjectType implements Serializable {
FOLDER, REGISTER, DOCUMENT
}
EcmField
public class EcmField implements Comparable, Serializable {
private final EcmFieldDataType dataType;
private final EcmFieldControlType controlType;
private final String name;
private final String dbname;
private final String internalname;
private final Integer length;
// getter, equals, hashCode
}
EcmFieldDataType
public enum EcmFieldDataType implements Serializable {
TEXT, DATE, NUMBER, GROUP, DEC;
}
and EcmFieldControlType
public enum EcmFieldControlType implements Serializable{
DEFAULT, CHECKBOX, LIST, DBLIST, TEXTAREA, HIERARCHY, TREE, GRID, RADIO, PAGECONTROL, STATIC;
}
I have overwritten all hashCode and equal methods by usind commons lang's EqualsBuilder and HashCodeBuilder. Now when I copy a A HashMap this way:
Map<String, Map<String, EcmObject>> m = EcmUtil.convertXmlObjectDefsToEcmEntries(new File("e:\\objdef.xml"));
Map<String, Map<String, EcmObject>> m2;
System.out.println(m.hashCode());
ByteArrayOutputStream baos = new ByteArrayOutputStream(8 * 4096);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(m);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
m2 = (Map<String, Map<String, EcmObject>>) ois.readObject();
System.out.println(m.hashCode());
System.out.println(m2.hashCode());
m.hashCode() is not equal to m2.hashCode()
here is my output:
-1639352210
-2071553208
1679930154
Another strange thing is, that eg. 10 times m has the same hashcode and suddenly on the 11th time the hashcode is different...
Any ideas what this is about?
© Stack Overflow or respective owner