Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an unique hashCode based on many values

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...

like image 443
Guillem Avatar asked Jan 27 '23 22:01

Guillem


1 Answers

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.

like image 126
Eran Avatar answered Feb 20 '23 10:02

Eran