Closed
Description
The following mapValues snippet might be expected to return true:
val aMap = Map(0->0).mapValues(y => ((x: Int) => x + y)); aMap.hashCode == aMap.hashCode
However it returns false where I have tested it:
- Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131)
- Scala 2.12.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112)
(A trivialized example of a problem that did affect our production code.)
The above behaviour violates the Java documentation on hashCode : https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer
For anyone else bothered by this behaviour, applying .view.force to the mapValues result produces a real map with a consistent hashCode, at the cost of evaluating and storing the mapValues expression for every value in the original map.