title: HashMap serialization and deserialization changes
body: <p>We are working with an in memory data grid (IMDG) and we have a migration tool. In order to verify that all the objects are migrated successfully, we calculate the chucksum of the objects from its serialized version.</p>
  
  <p>We are seeing some problems with HashMap, where we serialize it, but when we deserialize it the checksum changes. Here is a simple test case:</p>
  
  <pre><code>@Test
  public void testMapSerialization() throws IOException, ClassNotFoundException {
      TestClass tc1 = new TestClass();
      tc1.init();
      String checksum1 = SpaceObjectUtils.calculateChecksum(tc1);
  
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutput out = null;
      byte[] objBytes = null;
      out = new ObjectOutputStream(bos);
      out.writeObject(tc1);
      objBytes = bos.toByteArray();
      out.close();
      ByteArrayInputStream bis = new ByteArrayInputStream(objBytes);
      ObjectInputStream in = new ObjectInputStream(bis);
      TestClass tc2 = (TestClass) in.readObject();
      String checksum2 = SpaceObjectUtils.calculateChecksum(tc2);
  
      assertEquals(checksum1, checksum2);
  }
  </code></pre>
  
  <p>The TestClass looks like this:</p>
  
  <pre><code>class TestClass implements Serializable {
      private static final long serialVersionUID = 5528034467300853270L;
  
      private Map&lt;String, Object&gt; map;
  
      public TestClass() {
      }
  
      public Map&lt;String, Object&gt; getMap() {
          return map;
      }
  
      public void setMap(Map&lt;String, Object&gt; map) {
          this.map = map;
      }
  
      public void init() {
          map = new HashMap&lt;String, Object&gt;();
          map.put("name", Integer.valueOf(4));
          map.put("type", Integer.valueOf(4));
          map.put("emails", new BigDecimal("43.3"));
          map.put("theme", "sdfsd");
          map.put("notes", Integer.valueOf(4));
          map.put("addresses", Integer.valueOf(4));
          map.put("additionalInformation", new BigDecimal("43.3"));
          map.put("accessKey", "sdfsd");
          map.put("accountId", Integer.valueOf(4));
          map.put("password", Integer.valueOf(4));
          map.put("domain", new BigDecimal("43.3"));
      }
  }
  </code></pre>
  
  <p>And this is the method to calculate the checksum:</p>
  
  <pre><code>public static String calculateChecksum(Serializable obj) {
      if (obj == null) {
          throw new IllegalArgumentException("The object cannot be null");
      }
      MessageDigest digest = null;
      try {
          digest = MessageDigest.getInstance("MD5");
      } catch (java.security.NoSuchAlgorithmException nsae) {
          throw new IllegalStateException("Algorithm MD5 is not present", nsae);
      }
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutput out = null;
      byte[] objBytes = null;
      try {
          out = new ObjectOutputStream(bos);
          out.writeObject(obj);
          objBytes = bos.toByteArray();
          out.close();
      } catch (IOException e) {
          throw new IllegalStateException(
                  "There was a problem trying to get the byte stream of this object: " + obj.toString());
      }
      digest.update(objBytes);
      byte[] hash = digest.digest();
      StringBuilder hexString = new StringBuilder();
      for (int i = 0; i &lt; hash.length; i++) {
          String hex = Integer.toHexString(0xFF &amp; hash[i]);
          if (hex.length() == 1) {
              hexString.append('0');
          }
          hexString.append(hex);
      }
      return hexString.toString();
  }
  </code></pre>
  
  <p>If you print the maps of tc1 and tc2, you can see that the elements are not in the same place:</p>
  
  <pre><code>{accessKey=sdfsd, accountId=4, theme=sdfsd, name=4, domain=43.3, additionalInformation=43.3, emails=43.3, addresses=4, notes=4, type=4, password=4}
  {accessKey=sdfsd, accountId=4, name=4, theme=sdfsd, domain=43.3, emails=43.3, additionalInformation=43.3, type=4, notes=4, addresses=4, password=4}
  </code></pre>
  
  <p>I would like to be able to serialize the HashMap and get the same checksum when I deserialize it. Do you know if there is a solution or if I'm doing something wrong?</p>
  
  <p>Thanks!</p>
  
  <p>Diego</p>