Why does .hashCode() return different values for the same input?
"ABC".getBytes().hashCode()

It is because the hashCode of an Array doesn't depend on its values (because it is mutable) but rather it is unique for each instance, it uses its memory address. So executing the code twice creates tow different arrays that have different memory addresses that have different hash codes.
The solution is to use an immutable structure, like a List.
"ABC".getBytes().toList.hashCode()
// res: Int = 1984571950
"ABC".getBytes().toList.hashCode()
// res: Int = 1984571950
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With