title: How to override hashCode and equals methods in serialized object?
body: <p>I am using a <code>NamedCache.get(cacheKey)</code> and <code>NamedCache.put(cacheKey, cacheValue)</code> and overriding <code>hashCode</code> and <code>equals</code> method in a <code>cacheKey</code> object, also making the <code>cacheKey</code> object Serializable. But the <code>hashCode</code> and <code>equals</code> method isn't getting called.</p>
  
  <p>Why is this happening?</p>
  
  <p>I am posting a sample code for the above question</p>
  
  <pre><code>import java.io.Serializable;
  import java.util.HashMap;
  import java.util.Map;
  
  import com.tangosol.net.CacheFactory;
  import com.tangosol.net.NamedCache;
  
  class HelloEx implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 6280029994647038136L;
      String str  = "HelloEx" ; 
  
      HelloEx(String s){
          str = s ; 
      }
  
  
      public int hashCode() {
          System.out.println("Inside HashCode");
          return 1;
  
      }
  
  
      public boolean equals(Object  o) {
          HelloEx s = (HelloEx)o ;
          if(str.equals(s.str)){
              System.out.println("Both are equal");
              return true; 
          }
          return false;
      }
  
  
  }
  
  
  public class HashSetEg {
  
      public static void main(String[] args) {
  
          HelloEx hEx4 = new HelloEx("Four");
          HelloEx hEx5 = new HelloEx("Five");
          HelloEx hEx6 = new HelloEx("Six");
  
          Map hp = new HashMap();
          hp.put(hEx4,hEx4); 
          hp.put(hEx5,hEx5);
          hp.put(hEx6,hEx6);
          hp.put(hEx6,hEx6);
          System.out.println("Test");
          hp.get(hEx6);
          hp.put(new String("aa"),new String("aa"));
          System.out.println("Test");
          //System.out.println("HashSet " +  hp  );
  
  
              NamedCache aggCache  = CacheFactory.getCache("MyCache");
          aggCache.put(hEx4,hEx4);
          aggCache.put(hEx6,hEx6);
          aggCache.put(hEx6,hEx6);
          System.out.println("End");
      }
  }
  </code></pre>
  
  <p>The output for the above code is:</p>
  
  <p>Inside HashCode</p>
  
  <p>Inside HashCode</p>
  
  <p>Inside HashCode</p>
  
  <p>Inside HashCode</p>
  
  <p>Test</p>
  
  <p>Inside HashCode</p>
  
  <p>Test</p>
  
  <p>End</p>
  
  <p>This code is using oracle coherence technology.So <strong>NamedCache</strong> is an interface inside <strong>coherence.jar</strong> which is used to hold the reference of created cache on coherence server which will be used by application and "<strong>CacheFactory.getCache("MyCache")</strong>" is used to create a cache on coherence server with name MyCache and in this cache we store key-value pairs just like we store data in HashMap</p>