Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8205150

HashMap uses .equals() method on key object during put operation

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      I was testing the concept mentioned in Synopsis line. As I see this seems correct with Wrapper class but not with user-defined class.
      My understanding is: While put operation HashMap checks keys with .equals() (content of key object) method and if it is same it replaces existing Object for that key.
      But below program is not confirming this with user-defined class's object.
      Please, correct if I am missing something :

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Step 1:
      Check by running code provided in '' section. Issue will be replicated.

      Step 2:
      Use @Override for equals method getting below error at compile time:
      >javac Test.java
      Test.java:41: error: method does not override or implement a method from a supertype
              @Override
              ^
      1 error


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      With Wrapper classes .equals() method working. :
      {20=b, 10=a_new}

      With user defined classes .equals() method is not working. :
      {T@1=a_new, T@1=b}
      ACTUAL -
      With Wrapper classes .equals() method working. :
      {20=b, 10=a_new}

      With user defined classes .equals() method is not working. :
      {T@1=a_new, T@1=b, T@1=a}

      ---------- BEGIN SOURCE ----------
      import java.util.*;
       
      class Test
      {
          public static void main(String... s1)
          {
              HashMap<Integer,String> m = new HashMap<>();
              m.put(new Integer(10),"a");
              m.put(new Integer(20),"b");
              m.put(new Integer(10),"a_new");
               
              System.out.println("With Wrapper classes .equals() method working. :");
              System.out.println(m);
               
              System.out.println();
               
              HashMap<T,String> m1 = new HashMap<>();
              m1.put(new T("10"),"a");
              m1.put(new T("20"),"b");
              m1.put(new T("10"),"a_new");
              System.out.println("With user defined classes .equals() method is not working. :");
              System.out.println(m1);
          }
      }
       
      class T extends Object
      {
          String s;
          T t1;
           
          T()
          {
               
          }
          T(String s)
          {
              this.s = s;
              this.t1 = new T();
          }
               
          public boolean equals(T tIn)
          {
              return this.s.equals(tIn.s);
          }
           
          @Override
          public int hashCode()
          {
              return 1;
          }
      }
      ---------- END SOURCE ----------

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: