I define my class as:
final class Key<T extends Comparable<T>> {
private final T q;
private final T o;
public Key(T q1, T o1) {
q = q1;
o = o1;
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Key) {
Key<T> s = (Key<T>)obj;
return q.equals(s.q) && o.equals(s.o);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(q,o);
}
}
I also define an array to contain object key . For example:
Object arr[] = new Object[100];
Key<String> k = new Key<>("a","b");
int h = k.hashcode();
...
arr[h+i % h] = k; //i from 1 to 10 for example
The problem is that hashcode() can return a negative value so
arr[h+i % h] = k;
can return an error out of array index. That's why I changed my code as(based on my searching for avoiding hashcode() return negative value):
@Override
public int hashCode() {
return (Objects.hash(q,o)&0x7FFFFFFF);
}
So if I do this way, does a uniform distribution of the hashcode() be changed or not? I mean the probability to have a same value from two different objects will be increased or not?