3 ms·
> the standard templates for intellij could use some work when it comes to the quality of hashCode; Doesn't everyone just use Objects::hash?
by vips7L 2y ago
> the standard templates for intellij could use some work when it comes to the quality of hashCode;
Doesn't everyone just use Objects::hash?
- xxs 2y ago>Doesn't everyone just use Objects::hash? I hope they do know better, it's actually rather poor. 1st it should not be used for a single parameter - just use hashCode() directly and ensure non-null in the c-tor (or Objects.hashCode). Then, in general you need - only few fields to calc a decent high cardinality hashCode... Last: 31 as a constant (used in most mul/add schemas by java). It's a single byte prime, so it has some benefits. It can be implemented via shift and add (x<<5 - x) but modern CPUs implement mul quite well and don't have carry flag deps so shift+add is worse in general. However, as a constant is more like to cause collisions in a general purpose hash function. It's close to being power of 2 and multiple of 10 -> 32 and 30.