Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Advanced
Jan 25, 1999

Where is That Serialized Hashed Key?

When serializing a Hashtable in Java, make sure that the keys used to store objects are either primitives, or the key class' hashCode() method overrides the superclass Object's hashCode() method. Otherwise, you may come across a problem when serializing a Java's Hashtable object as in the following code excerpt:
 
1.   Hashtable ht = new Hashtable();
2. 
3.   ht.put(new Key(), "One");
4.   System.out.println("v1 = " + (String)(ht.get(key1));
5. 
6.   try {
7.      // Write out Hashtable object
8.      FileOutputStream fos = new FileOutputStream("myObject.ser");
9.      ObjectOutputStream oos = new ObjectOutputStream(fos);
10.   oos.writeObject(ht);
11.   oos.flush();
12.   oos.close();
13. 
14.   // Read back Hashtable object
15.   FileInputStream fis = new FileInputStream("myObject.ser");
16.   ObjectInputStream ois = new ObjectInputStream(fis);
17.   Hashtable o = (Hashtable)(ois.readObject());
18.   ois.close();
19. 
20.   // Check if the object is in the Hashtable
21.   if (o.contains("One"))
22.     System.out.println("Object found in deserialized hashtable");
23. 
24.   String mv2 = (String)(o.get(key1));
25.   if (mv2 == null)
26.     System.out.println("Object not found !!!");
27. 
28. }
29. catch (Exception e) {
30.   e.printStackTrace();
31. }
Key is a class that implements Serializable. Objects of type Key are used to store elements in the Hashtable ht. On Line 3, the String "One" is stored in ht. Line 4 confirms that the object can be retrieved from ht. Lines 8-12 serialize ht to a file myObject.ser. Lines 15-18 read back the object into a variable o.

So far so good. At this point, you should be able to retrieve the String object "One" from the deserialized Hashtable o. Line 21 confirms that "One" is indeed an element in Hashtable o. If you are running this code in a program, the message on Line 22 should be printed out. However, when the program tries to retrieve the element on Line 24, it fails to do so and the message on Line 26 is printed out. What happened?

Well, remember that the Hashtable only stores objects, and in Java that means object references. When the object is deserialized, the JVM allocates a different reference for the first key for the Hashtable. Hence, even though the contents of the key may be the same as the ones for key1, the references are different. To avoid this problem, you should make sure that the keys used to store objects are either primitives, or the key class' hashCode() method overrides the superclass Object's hashCode() method.

Ajit Sagar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date