Java Hashtable .containsKey(String key) is returning true even when the strings and hashcodes are different... How? -
i'm having issues hashtable in java, feightpuzzle class i've created.
inside class have string storing key each instance. during program when check inside hashtable duplicate instances "find" when found ones different.
take example when call bol.containskey(current.key) bol ht , current feightpuzzle.
when true check values of keys , are
current.key = "8 14 11 0 6 12 13 1 10 4 5 9 15 2 3 7" bol.get(current.key).key = "12 8 4 0 13 9 5 1 14 10 6 2 15 11 7 3" with values
current.key.hashcode() = -950607924 bol.get(current.key).key.hashcode() = -1856769042 i'm sorry bother problem getting me, , last thing expected tonight honest (don't love that)... hints or answers appreciated!
i've reread question, , understand have following problem:
you
bol.containskey(current.key) to check if current in bol.
when returns true, expect value mapped current.key should indeed current, hash-codes indicate, it's not.
the problem 1 of following:
you didn't put puzzle object in hashtable correctly in first place.
you should do
bol.put(somepuzzle.key, somepuzzle)you changed key when puzzle in map. not allowed.
after you've added entry in map, may not change key without removing / reinserting mapping.
the hashtable object, based on key provided when inserting.
you've accidentally provided same key multiple different puzzle objects (in case, 1
putoverride previous one)
one suggestion let feightpuzzle override hashcode , equals , use hashset instead of hashtable.
Comments
Post a Comment