I am trying to implement an unique hashCode based on six different values. My Class has the following attributes:
private int id_place;
private String algorithm;
private Date mission_date;
private int mission_hour;
private int x;
private int y;
I am calculating the hashCode as following:
id_place * (7 * algorithm.hashCode()) + (31 * mission_date.hashCode()) + (23 * mission_hour + 89089) + (x * 19 + 67067) + (y * 11 + 97097);
How can I turn it into an unique hashCode? I'm not confident it is unique...
It doesn't have to be unique and it cannot be unique. hashCode()
returns an int
(32 bits), which means it could be unique if you only had one int
property and nothing else.
The Integer
class can (and does) have a unique hashCode()
, but few other classes do.
Since you have multiple properties, some of which are int
, a hashCode()
that is a function of these properties can't be unique.
You should strive for a hasCode()
function that gives a wide range of different values for different combinations of your properties, but it cannot be unique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With