从 HashMap 获取值的问题
大家好,我正在使用 HashMap 来保存带有字符串键的对象之一。当我用钥匙放置一个对象时,它没有问题,当我放置第二个对象时,我添加了我的对象,但无法用它的钥匙获取它。在某种程度上它会去“下一个”的地方。我从调试模式(eclipse)中截取了一个屏幕截图,下面
虽然大小显示 2,但我看不到我在哈希映射中的第二项,但在其他哈希映射的下一个节点中。
需要注意的是,我使用我的密钥,例如“name.tag”形式,标签和名称同时不能相同,但“tag”可以相同。在评估键时,hashmap 与点运算符有关吗?我希望我能写清楚,
提前致谢
编辑: 这是我用来创建哈希图的一段代码
private HashMap<String,ParameterItem> parseParametersNode(DataModel parent,Element element){
NodeList parameterChilds=element.getChildNodes();//gep element parameters
HashMap<String, ParameterItem> parameterItems=new HashMap<String, ParameterItem>();
for(int i=0;i<parameterChilds.getLength();i++){
if(parameterChilds.item(i).getNodeType()==Node.ELEMENT_NODE){
Element el=(Element) parameterChilds.item(i);
NamedNodeMap atts=el.getAttributes();
ParameterItem item=new ParameterItem();
for(int j=0;j<atts.getLength();j++){
Attr attribute=(Attr) atts.item(j);
String attributeValue=attribute.getValue();
String attributeName=attribute.getName();
item.setParsedProperty(attributeName, attributeValue);
} /*check attributes later*/
//finish loop and insert paramitem to params
String key="key"+i;
if(item.getTag()!=null && item.getName()!=null)
key=item.getName()+"."+item.getTag();
parameterItems.put(key, item);
// testParam=item;
// parameterItems.put(key, testParam);
}
}
return parameterItems;
}
Hi all I'm using a HashMap to hold one of my object with a string key. when I put an object with a key it has no problem, when I put my second object I got my object added but can't get it with its key. Somewhat it goes to somewhere that is "next". I took a screenshot from debug mode (eclipse), below
although size shows 2, I can't see my second item in hashmap, but in other hashmap's next node.
To note something I use my key like in a form "name.tag", tag and name in same time can never be the same, but "tag" can be the same. does hashmap has something to do with dot operator when evaluating keys? I hope I could write clearly,
Thanks in advance
Edit:
Here is a piece of code I use to create my hashmap
private HashMap<String,ParameterItem> parseParametersNode(DataModel parent,Element element){
NodeList parameterChilds=element.getChildNodes();//gep element parameters
HashMap<String, ParameterItem> parameterItems=new HashMap<String, ParameterItem>();
for(int i=0;i<parameterChilds.getLength();i++){
if(parameterChilds.item(i).getNodeType()==Node.ELEMENT_NODE){
Element el=(Element) parameterChilds.item(i);
NamedNodeMap atts=el.getAttributes();
ParameterItem item=new ParameterItem();
for(int j=0;j<atts.getLength();j++){
Attr attribute=(Attr) atts.item(j);
String attributeValue=attribute.getValue();
String attributeName=attribute.getName();
item.setParsedProperty(attributeName, attributeValue);
} /*check attributes later*/
//finish loop and insert paramitem to params
String key="key"+i;
if(item.getTag()!=null && item.getName()!=null)
key=item.getName()+"."+item.getTag();
parameterItems.put(key, item);
// testParam=item;
// parameterItems.put(key, testParam);
}
}
return parameterItems;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里并没有真正的问题:您遇到了哈希冲突。也就是说,您的两个密钥都已放置在同一个哈希桶中。看来您只有 4 个存储桶(奇怪,我认为最初的默认值是 10 或 16),因此随机数据出现这种情况的可能性是 25%。你的尺寸增加得很好。
next
是内部实现指向同一存储桶中的下一个元素的方式。如果每个桶中的元素数量太大,Java 将在内部重新散列到更多桶中。我不明白为什么你需要一个哈希表,因为你要连续对键进行编号(你可以使用 ArrayList ),但也许这只是入门代码,你的实际用例是不同的。
There is not really a problem here: you have a hash collision. That is, both of your keys have been placed in the same hash bucket. It appears you have only four buckets (odd, I thought the initial default was 10 or 16), so the chance of that with random data is 25 percent. Your size incremented just fine. The
next
is the internal implementation’s way of pointing to the next element in the same bucket. If the number of elements in each buckets gets too big, Java will internally rehash into more buckets.I do not see why you need a HashTable here since you are numbering your keys consecutively (you could use an
ArrayList
), but maybe this is just starter code and your real use case is different.您有代码:
但在此之后您再次设置密钥而不添加到它:
应该是 key +=item.getName()+"."+item.getTag(); ?
You have the code:
but right after this you set key again not adding to it:
Should this be key +=item.getName()+"."+item.getTag(); ?