holgerbrandl
08/07/2019, 11:44 PMprintln(listOf(3, 263).hashCode())
println(listOf(5, 201).hashCode())
// -> 1317
println(Arrays.asList(3, 263).hashCode())
println(Arrays.asList(5, 201).hashCode())
// --> 40830
It's a more general problem as also the java example suffers from exactly the same problem. Interestingly, arrays with the same data give differnt hashes as documented at https://github.com/holgerbrandl/krangl/issues/75ilya.gorbunov
08/07/2019, 11:50 PMhashCode
function converts an infinite range of input values to the limited range of Int
values, there could certainly be two lists that produce the same hash code value.ilya.gorbunov
08/08/2019, 12:01 AM((1 * 31) + element1.hashCode) * 31) + element2.hashCode
, and an Int value's hash code is that value itself.
Thus (1 * 31 + 3)*31 + 263
is the same as (1 * 31 + 5)*31 + 201
Dominaezzz
08/08/2019, 12:14 AMDominaezzz
08/08/2019, 12:19 AMhashCode
, which is why they are different.gildor
08/08/2019, 4:57 AMkarelpeeters
08/08/2019, 6:30 AMreturn 0
is a valid hashcode
implementation.holgerbrandl
08/08/2019, 6:45 AMLong
would give rise to an infinite number of collisions.holgerbrandl
08/08/2019, 6:47 AMkarelpeeters
08/08/2019, 6:49 AMgroupBy
from the stdlib.holgerbrandl
08/08/2019, 6:52 AMgroupBy
seems to be the way forward. My original motivation to reduce the data with hashCode
was better performance (without much thinking or benchmarking).