HashMap - 正确用法?
我正在尝试处理带有多个附件的彩信。为此,我创建了一个 HashMap,如下所示(这不是完整的实现,只是相关部分):
HashMap<String, Integer> hashAttachments = new HashMap<String, Integer>();
int c = 0;
if(atts != null) {
for(Attachment a : atts){
if(a.mimeType.startsWith("image/")){
<some code here>
hashAttachments.put(a.fileName, indx);
}else if(a.mimeType.startsWith("text/")){
<some code here>
hashAttachments.put("text_"+String.valueOf(c++)+".txt",indx);
}
<some more mime types>
} /* for */
我尝试处理的消息有 4 个附件 - 两个图像和两个文本,所以我希望哈希映射包含 4 个附件for 循环结束时的条目。
我实际看到的是,在某个时刻,地图的一个条目被覆盖,我最终得到 3 个条目,而不是 4 个。可能是什么原因? (键是唯一的,在所有情况下都不为空且不为空)
提前感谢
编辑:每次迭代后设置的键(看起来很完美,不是我在调试器中检查键时看到的):
10-16 21:50:01.207: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.207: INFO/System.out(27593): abc.jpg
10-16 21:50:01.207: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.217: INFO/System.out(27593): abc.jpg
10-16 21:50:01.217: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.227: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.227: INFO/System.out(27593): abc.jpg
10-16 21:50:01.227: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.227: INFO/System.out(27593): text_0.txt
10-16 21:50:01.237: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.237: INFO/System.out(27593): abc.jpg
10-16 21:50:01.237: INFO/System.out(27593): text_1.txt
10-16 21:50:01.237: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.237: INFO/System.out(27593): text_0.txt
i am trying to process an MMS message with multiple attachments. For that I'm creating a HashMap as following (this is not the complete implementation but only the relevant part):
HashMap<String, Integer> hashAttachments = new HashMap<String, Integer>();
int c = 0;
if(atts != null) {
for(Attachment a : atts){
if(a.mimeType.startsWith("image/")){
<some code here>
hashAttachments.put(a.fileName, indx);
}else if(a.mimeType.startsWith("text/")){
<some code here>
hashAttachments.put("text_"+String.valueOf(c++)+".txt",indx);
}
<some more mime types>
} /* for */
the message I am trying to process has 4 attachments - two images and two texts, so I expect the hash map to contain 4 entries when the for loop is over.
What I actually see is that at some point one of the entries of the map is overwritten and I end up with 3 entries instead of 4. What could be a reason? (keys are unique, not null and not empty in all cases)
Thanks in advance
EDIT: key set after each iteration (looks perfect, not what I saw inspecting keys in debugger):
10-16 21:50:01.207: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.207: INFO/System.out(27593): abc.jpg
10-16 21:50:01.207: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.217: INFO/System.out(27593): abc.jpg
10-16 21:50:01.217: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.227: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.227: INFO/System.out(27593): abc.jpg
10-16 21:50:01.227: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.227: INFO/System.out(27593): text_0.txt
10-16 21:50:01.237: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.237: INFO/System.out(27593): abc.jpg
10-16 21:50:01.237: INFO/System.out(27593): text_1.txt
10-16 21:50:01.237: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.237: INFO/System.out(27593): text_0.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于您的带时间戳的调试输出是否正确,请记住以下关于 HashMap 类的内容:
来自javadoc: http://download.oracle.com /javase/6/docs/api/java/util/HashMap.html
在调试器中,它可能看起来像是一个键/值对被覆盖,但实际上,插入可能改变了哈希映射。测试 put(...) 的返回值是测试新键/值是否与现有键冲突的最佳方法。
Based on you timestamped debug output being correct, please remember the following, regarding the HashMap class:
from the javadoc: http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
In the debugger, it may have looked like a key/value pair was being overwritten, but in reality, the insertion probably changed the order of the HashMap. Testing the return value of put(...) is the best way to test if a new key/value is colliding with an existing key.