I have a method that is expected to save an object in a hashmap (used as a cache) that has as a key a String
.
The objects that are passed in the method have either fields that are “volatile” i.e. they can change on a next refresh from the data store or are the same across all objects except for 3 fields.
Those fields are 2 of type double
and 1 field of type String
.
What I am doing now is I use:
Objects.hash(doubleField1, doubleField2, theString)
and convert the result to String
.
Basically I am generating a key for that object based on the immutable state.
This seems to work but I have the following question:
If we exclude the case that we have 2 objects with the exact same fields (which is impossible) how likely is that I could end up with a collision that won’t be able to be verified properly?
I mean that if I have a hashmap with keys e.g. strings etc if there is a collision on the hashCode the actual value of the key is compared to verify if it is the same object and not a collision.
Would using keys the way I have described create problems in such verification?
Update:
If I have e.g. a hashmap with key a Person
and the hashCode
is generated using fullName
and ssn
or dateOfBirth
if there is a collision then the hashmap implementation uses equals
to verify if it is the actual object being searched for.
I was wondering if the approach I describe could have some issue in that part because I generate the actual key directly