I thought this is why two objects should be !=, but be equals() at the same time.
It's the way hashing works. A hashmap. hashset, etc., will first use the hash to get to a bucket, then use the equals() method to compare with the elements in the same bucket, rejecting if it is a duplicate.
If the hashcodes are different, they go to different buckets, and the equals() methods is never called. In effect, you have place two "equal" objects into a collection that doesn't allow duplicates... and of course, violated the hashcode/equals contract.
Its worth noting that " if two objects are equal with equals() method there hashcode must be same. but two unequal object can have same hashcode " so if that's the case it will lead frequent collision if you use that object as key in hashMap or Hashtable and that's why objects like String and wrapper class are good HashKey because for them if two objects are unequal there hashcode also differ.