简体   繁体   English

HashMap没有添加所有密钥

[英]HashMap not adding all keys

I am adding strings to a HashMap<String, ChartSeries> but it doesn't add all the strings. 我正在将字符串添加到HashMap<String, ChartSeries>但不会添加所有字符串。 It does however change the size integer of the hashmap: 但是,它确实更改了哈希图的大小整数: 在此处输入图片说明

As you can see it says size:6 but when you look into the table it only holds 4 objects. 如您所见,它说的是size:6但是当您查看表格时,它仅包含4个对象。

This is my code: 这是我的代码:

for (CaseTypeActivationAmount CTAM : caseTypeActivationAmounts) {
    ChartSeries cs;
    if (!caseTypes.containsKey(CTAM.getOmschrijving())) {
        if (CTAM.getOmschrijving() != null) {
            cs = new ChartSeries(CTAM.getOmschrijving());
        } else {
            cs = new ChartSeries(" ");
        }

        caseTypes.put(CTAM.getOmschrijving(), cs);
    } else {
        cs = caseTypes.get(CTAM.getOmschrijving());
    }
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
    cs.set(dateFormat.format(CTAM.getDate()), CTAM.getAmount());
}

Am I missing something? 我想念什么吗?

when you look into the table it only holds 4 objects. 当您查看表格时,它仅包含4个对象。

The table does hold six objects. 该表确实包含六个对象。 However, due to hash collisions, it holds them in only four separate chains . 但是,由于哈希冲突,它仅将它们保持在四个单独的链中

You can see what's going on in the debugger: open the value of each node, and examine the next value. 您可以看到调试器中正在发生的情况:打开每个节点的值,然后检查下一个值。 You will find that either (1) two of the six nodes have a second item, or (2) one of the six nodes has two additional items in its chain, for the overall count of six objects. 您将发现(1)六个节点中的两个具有第二项,或者(2)六个节点中的一个在其链中具有两个附加项,以计算六个对象的总数。

Yes, you are missing something. 是的,您缺少一些东西。 Each position of the backing array of the HashMap can hold a reference to a single HashMap.Node . HashMap的后备数组的每个位置都可以包含对单个HashMap.Node的引用。 However, that Node does not necessarily contain just a single Map.Entry . 但是,该Node不一定只包含一个Map.Entry It can contain a linked-list or tree of Entries which were mapped to the same bucket (bit) of the HashMap . 它可以包含映射到HashMap的同一存储桶(位)的条目的链接列表或条目树。

Therefore the 4 Node s you are seeing do not correspond with 4 Entries. 因此,您看到的4个Node与4个条目不对应。

If you iterate over the entrySet of the map you'll see all 6 Entries. 如果您遍历地图的entrySet ,将看到所有6个条目。

Those are Node s, actually LinkedNodes. 这些是Node ,实际上是LinkedNodes。 The hashcode of some of the keys are equal, and you get more then one entry in the same bucket. 一些键的哈希码是相等的,在同一个存储桶中,您将获得一个以上的条目。

One of the HashMap$Node that you see is actually : 您看到的HashMap$Node之一实际上是:

 HashMap$Node -> HashMap$Node(next)...

Here is how it actually looks internally: actually内部外观:

static class Node<K,V> implements Map.Entry<K,V> {
    final int hash;
    final K key;
    V value;
    Node<K,V> next; // NOTICE the next here

When you go after a certain limit, those Nodes are going to become TreeNodes (holding an entire Tree of Entries) in a single bucket. 当你去after一定限度时,这些节点将成为树节点在一个桶(保持整个条目树)。

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

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