Skip to main content
added 280 characters in body; deleted 9 characters in body
Source Link
oiavorskyi
  • 2.9k
  • 1
  • 22
  • 23

In this case it's not about hashCode() but is about equals() method. HashSet is still Set, which has semantic of not allowing duplicates. Duplicates are checked for using equals() method which in case of String will return true

However for your test class equals() method is not defined and it will use the default implementation from Object which will return true only when both references are to the same instance.

Method hashCode() is used not to check if objects should be treated as same but as a way to distribute them in collections based on hash functions. It's absolutely possible that for two objects this method will return same value while equals() will return false.

P.S. hashCode implementation of Object doesn't guarantee uniqueness of values. It's easy to check using simple loop.

In this case it's not about hashCode() but is about equals() method. HashSet is still Set, which has semantic of not allowing duplicates. Duplicates are checked for using equals() method which in case of String will return true

However for your test class equals() method is not defined and it will use the default implementation from Object which will return true only when both references are to the same instance.

P.S. hashCode implementation of Object doesn't guarantee uniqueness of values. It's easy to check using simple loop.

In this case it's not about hashCode() but is about equals() method. HashSet is still Set, which has semantic of not allowing duplicates. Duplicates are checked for using equals() method which in case of String will return true

However for your test class equals() method is not defined and it will use the default implementation from Object which will return true only when both references are to the same instance.

Method hashCode() is used not to check if objects should be treated as same but as a way to distribute them in collections based on hash functions. It's absolutely possible that for two objects this method will return same value while equals() will return false.

P.S. hashCode implementation of Object doesn't guarantee uniqueness of values. It's easy to check using simple loop.

added 19 characters in body
Source Link
oiavorskyi
  • 2.9k
  • 1
  • 22
  • 23

In this case it's not about hashCode() but is about equals() method. HashSet is still Set, which has semantic of not allowing duplicates. Duplicates are checked for using equals() method which in case of String will return true

However for your test class equals() method is not defined and it will use the default implementation from Object which will return true only when both objectsreferences are to the same instance.

P.S. hashCode implementation of Object doesn't guarantee uniqueness of values. It's easy to check using simple loop.

In this case it's not about hashCode() but is about equals() method. HashSet is still Set, which has semantic of not allowing duplicates. Duplicates are checked for using equals() method which in case of String will return true

However for your test class equals() method is not defined and it will use the default implementation from Object which will return true only when both objects are same.

P.S. hashCode implementation of Object doesn't guarantee uniqueness of values. It's easy to check using simple loop.

In this case it's not about hashCode() but is about equals() method. HashSet is still Set, which has semantic of not allowing duplicates. Duplicates are checked for using equals() method which in case of String will return true

However for your test class equals() method is not defined and it will use the default implementation from Object which will return true only when both references are to the same instance.

P.S. hashCode implementation of Object doesn't guarantee uniqueness of values. It's easy to check using simple loop.

Source Link
oiavorskyi
  • 2.9k
  • 1
  • 22
  • 23

In this case it's not about hashCode() but is about equals() method. HashSet is still Set, which has semantic of not allowing duplicates. Duplicates are checked for using equals() method which in case of String will return true

However for your test class equals() method is not defined and it will use the default implementation from Object which will return true only when both objects are same.

P.S. hashCode implementation of Object doesn't guarantee uniqueness of values. It's easy to check using simple loop.