Where is That Serialized Hashed Key?

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 object8.      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 object15.   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 Hashtable21.   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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes