javahashtrove4j

Trove hash maps not implementing hashCode


I stumbled across the issue with TObjectDoubleHashMap of trove. The version 2.0.2 doesn't override the hashCode method.

More recent versions of trove (3.0.0) include the hashCode.

Note, that the equals method is overriden in both versions.

Why wasn't the hashCode method overriden in the version 2? Maybe the contract for overriding hashCode/equals was introduced after the trove maps were written originally? Or is it simply a violation of the contract and a bug that was fixed later? I'd be very surprised if it's a bug and not a feature, because how come the class that mostly deals with hashing not override hashCode when it has equals


Solution

  • Maybe the contract for overriding hashCode/equals was introduced after the trove maps were written originally?

    No. That was present way back. I believe at least as early as 1.1, but I can't find anything earlier than 1.3 docs.

    Or is it simply a violation of the contract and a bug?

    Yes. It is a violation of the contract. Note that java.util.Map has its own extra equality/hashCode contract above and beyond java.lang.Object, which is knowingly violated by IdentityHashMap and a few others.

    From the javadoc:

    public boolean equals(Object o)
    

    Compares the specified object with this map for equality. Returns true if the given object is also a map and the two Maps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings if t1.entrySet().equals(t2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.