我有这样的结构:为什么我从字符串中获取错误的HashCode?
private HashMap<Integer,HashMap<Object,Integer>> commandList= new HashMap<>();
填充这样:
{1={1=2, 2=3, 3=4, -999=-999, -998=-998}}
从这个代码:
if ((msgTypeTemp=commandList.get(this.msgType).get(msgContent))==null) {
Object s= "1";
System.out.println("Class of s: "+s.getClass().getSimpleName()+"\nClass of msgContent: "+msgContent.getClass().getSimpleName());
System.out.println("msgMap:\n"+msgMap);
System.out.println("commandList:\n"+commandList);
System.out.println(s.hashCode());
System.out.println(msgContent.hashCode());
System.out.println(commandList.get(this.msgType).get(s));
this.msgType=JSockOS_UndefinedMsg.MSG_CODE;
specialMsg=true;
} else {
this.msgType=msgTypeTemp;
if (specialMsgType(this.msgType)){
specialMsg=true;
}
}
我HashMap
是通用型<String,Integer>
但是,每当我CA得到msgContent
上的get方法,结果发现代替"1"
的散列代码,它是一个散列代码,它直到被设置为0,然后在get方法调用之后改变了。
这仅发生于使用"msgContent"
参数调用...
如果我用这个:System.out.println(commandList.get(this.msgType).get(s));
它返回 “2” 果然......
看也是这个形象,它可能有帮助。
msgContent得到这样上面的代码之前改变:
这是第一:2.1。 然后它得到:1.
仍然是一个字符串。
msgContent=msgContent.toString().split(Pattern.quote("."))[1];
do(msgContent); // a methods which implements the code showed before.
//msgContent is a parameter, --> public void do(Object msgContent)
[编辑]: 发现问题:msgContent是495个字符...会解决它的变化和更新!
你确定内部'HashMap
我的散列表不会有相同类型的键,但每个键元素都会提供散列码和相同的方法来获得正确的散列码相关决定。 在这个例子中,我的hashmap仅包含键值中的字符串。事实上,我测试了这个试图通过键(字符串)“1”获取值。 msgContent也是一个字符串(在运行时),它包含“1”,但它似乎是一个不同的对象... –
在你发布的图像中,msgContent的细节是不完整的,好像它的值是“1”或稍微不同的东西,这会产生巨大的差异。 –