r/javahelp • • May 20 '20

Wouldn't the Eclipse's auto generated hashcode method go above max int value way too quickly?

@Overridepublic int hashCode() {
	final int prime = 31;
	int result = 1;

	result = prime * result + a;
	result = prime * result + b;
        result = prime * result + c;
	result = prime * result + d;

	return result;
}

Say a,b,c,d are only above 100, it would already return a number way above the max integer value. Does this hash function only have very limited uses or am I missing something?

19 Upvotes

5 comments sorted by

View all comments

7

u/Robyt3 May 20 '20

Aside from your question, which has been answered: You can just use Objects.hash(a, b, c, d); instead of generating the math and prime stuff in most cases.

1

u/ReligionIsAScam_ May 20 '20

Interesting! Appreciated