HashMap - 正确用法?

发布于 2024-09-27 17:49:21 字数 1691 浏览 1 评论 0原文

我正在尝试处理带有多个附件的彩信。为此,我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-10-04 17:49:21

基于您的带时间戳的调试输出是否正确,请记住以下关于 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:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文