previous | start | next

Problem with equals and hashCode

Suppose we override the equals method from Object but do not override the hashCode method.

Consider this code:

      TokenPos t1, t2;
      HashSet<TokenPos> hs = new HashSet<TokenPos>();
      t1 = new TokenPos("count", 3);
      t2 = new TokenPos("count", 3);

      hs.add(t1);
      if ( hs.contains(t2) ) {
         System.out.println("YES");
      } else {
         System.out.println("NO");
      }
   

What will be printed? Why?



previous | start | next