TObjectDoubleHashMap -> IllegalArgumentException: Equal objects must have equal hashcodes

Issue #19 invalid
Marek Grzes created an issue

The full error message is below. Please note that both objects are equal and their hash codes printed in this log message are equal as well. Hence, the exception was thrown for another reason. I am using trove-3.0.3.jar and java 1.7 in Ubuntu.

Exception in thread "main" java.lang.IllegalArgumentException: Equal objects must have equal hashcodes. During rehashing, Trove discovered that the following two objects claim to be equal (as in java.lang.Object.equals()) but their hashCodes (or those calculated by your TObjectHashingStrategy) are not equal.This violates the general contract of java.lang.Object.hashCode(). See bullet point two in that method's documentation. object #1 =class no.uib.cipr.matrix.sparse.SparseVector id= 6023412 hashCode= 13146907 toString= 20 19 1 1.251581290418e-02 2 3.941029249267e-02 3 1.835726115298e-02 4 2.944971125299e-03 5 3.483768304813e-03 6 5.674853088402e-02 7 2.950590494351e-02 9 1.303714497171e-01 10 2.565680063959e-02 11 1.408256624742e-01 12 2.805709294356e-02 13 1.736289276700e-03 14 7.879584042277e-03 15 4.339459900875e-02 16 2.093347031617e-01 17 5.505685022801e-02 18 5.197462767171e-02 19 1.187098972250e-01 20 2.403590180400e-02 ; object #2 =class no.uib.cipr.matrix.sparse.SparseVector id= 6023412 hashCode= 13146907 toString= 20 19 1 1.251581290418e-02 2 3.941029249267e-02 3 1.835726115298e-02 4 2.944971125299e-03 5 3.483768304813e-03 6 5.674853088402e-02 7 2.950590494351e-02 9 1.303714497171e-01 10 2.565680063959e-02 11 1.408256624742e-01 12 2.805709294356e-02 13 1.736289276700e-03 14 7.879584042277e-03 15 4.339459900875e-02 16 2.093347031617e-01 17 5.505685022801e-02 18 5.197462767171e-02 19 1.187098972250e-01 20 2.403590180400e-02

at gnu.trove.impl.hash.TObjectHash.buildObjectContractViolation(TObjectHash.java:464)
at gnu.trove.impl.hash.TObjectHash.throwObjectContractViolation(TObjectHash.java:426)
at gnu.trove.map.hash.TObjectDoubleHashMap.rehash(TObjectDoubleHashMap.java:191)
at gnu.trove.impl.hash.THash.postInsertHook(THash.java:388)
at gnu.trove.map.hash.TObjectDoubleHashMap.doPut(TObjectDoubleHashMap.java:265)
at gnu.trove.map.hash.TObjectDoubleHashMap.put(TObjectDoubleHashMap.java:240)

Comments (4)

  1. Marek Grzes reporter

    I think there was one thing wrong in my code that caused this exception. As long as I am quite sure that the hash and equal methods on the SparseVector objects are fine, I did one mistake when I was putting objects to TObjectDoubleHashMap. I did the following:

    SparseVector b = new ...

    map.put(b,value);

    then, I was modifying b (i.e., I was modifying its internal data where the pointer to b was the same) and after that I was adding it again (i.e., I was adding the same object with different data inside) using

    map.put(b,value2)

    once I changed the second put to:

    map.put(b.clone(), value2);

    then it worked fine and I do not have this exception any more.

    It seems to me that the problem was on my side. I am sorry for the false alarm but on the other hand my mistake could be helpful for you to provide more informative error messages for the future. I've been using trove for over a year now and I find it very useful.

  2. Log in to comment