简体   繁体   English

即使字符串和哈希码不同,Java Hashtable .containsKey(String key)也会返回true。

[英]Java Hashtable .containsKey(String key) is returning true even when the strings and hashcodes are different… How?

I'm currently having some issues with my Hashtable in java, where FEightPuzzle is a class which I've created. 我目前在Java中的Hashtable遇到一些问题,其中FEightPuzzle是我创建的类。

Inside my class I have a String which is storing the key for each instance. 在我的类中,我有一个字符串,它存储每个实例的密钥。 Now during my program when I check inside the Hashtable for duplicate instances I sometimes "find" some when really the found ones are different. 现在,在程序执行过程中,当我在Hashtable中检查重复的实例时,有时我会“发现”一些确实不同的实例。

Take for example when I call bol.containsKey(current.key) where bol is a HT and current is an FEightPuzzle. 例如,当我调用bol.containsKey(current.key)时,其中bol是HT,而current是FEightPuzzle。

When this is true I check the values of the keys and they 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 to bother you but this problem is really getting to me, and it was the last thing I expected tonight to be honest (don't you love that)... Any hints or answers would be greatly appreciated! 很抱歉打扰您,但是这个问题确实已经困扰我了,这是我今晚诚实的最后一件事(您不喜欢那个)...任何提示或答案将不胜感激!

I've reread your question, and as I understand it you have the following problem: 我已经重新阅读了您的问题,据我了解,您遇到以下问题:

You do 你做

bol.containsKey(current.key)

to check if current is already in bol . 检查current已在bol

When it returns true, you expect that the value mapped to by current.key should indeed be current , but as your hash-codes indicate, it's not. 当它返回true时,您期望由current.key映射的值确实应该是current ,但是正如您的哈希码所示,不是。

The problem is likely to be one of the following: 该问题可能是以下原因之一:

  1. You didn't put the puzzle object in the hashtable correctly in the first place. 首先,您没有正确地将拼图对象放入哈希表中。

    You should do 你应该做

     bol.put(somePuzzle.key, somePuzzle) 
  2. You changed the key when the puzzle was in the map. 当拼图在地图中时,您更改了键。 THIS IS NOT ALLOWED. 不允许这样做。

    After you've added the entry in the map, you may not change the key without removing / reinserting the mapping. 在地图中添加条目后,如果不删除/重新插入地图,则不能更改键。

    The Hashtable will look for the object, based on the key you provided when inserting. 哈希表将根据您在插入时提供的键来寻找对象。

  3. You've accidentally provided the same key for multiple different puzzle objects (in which case, one put will override a previous one) 您不小心为多个不同的拼图对象提供了相同的键 (在这种情况下,一个put将覆盖前一个put


One suggestion would be to let FEightPuzzle override hashCode and equals and use a HashSet instead of a Hashtable. 一种建议是让FEightPuzzle覆盖hashCode和equals并使用HashSet代替Hashtable。

I think perhaps you're misunderstanding what Hashtable does. 我认为您可能误解了Hashtable功能。 It maps keys to values. 它将键映射到值。 So calling get(key) on your key is going to return the value you provided with put(key, value) . 因此,在键上调用get(key)将返回您通过put(key, value)提供的put(key, value) Now if you're always putting the same value in as the key you should expect the same thing, however in that case all you need is a HashSet . 现在,如果您始终将相同的值用作键,则应该期待相同的结果,但是在这种情况下,您只需要一个HashSet If you're putting the same values in for different keys, it is going to allow that. 如果为不同的键输入相同的值,则将允许这样做。 Only the keys are unique in a Hashtable (and HashMap is the same). Hashtable只有键是唯一的(并且HashMap是相同的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM