简体   繁体   English

HashMap put方法

[英]HashMap put method

I'm having some trouble when using .put(Integer, String) in Java. 在Java中使用.put(Integer, String)时遇到一些麻烦。 To my understanding, when a collision happens the HashMap asks whether the to value are the same with .equals(Object) and if they are not the two values are stored in a LinkedList . 据我了解,当发生冲突时, HashMap询问to值是否与.equals(Object)相同,如果不是,则将这两个值存储在LinkedList Nevertheless, size() is 1 and the hash iterator only shows one result, the last one. 不过, size()为1,而散列迭代器仅显示一个结果,即最后一个结果。

Apart form this, java HashMap API states:put 除此之外,java HashMap API指出:put

 public V put(K key, V value) 

Associates the specified value with the specified key in this map. 将指定值与该映射中的指定键相关联。 If the map previously contained a mapping for the key, the old value is replaced. 如果该映射先前包含该键的映射,则将替换旧值。

THIS IS NOT WHAT I HAVE READ EVERYWHERE. 这不是我所阅读的所有内容。

Thoughts? 思考?

public class HashProblema {

    public static void main(String[] args) {
        HashMap<Integer, String> hash= new HashMap();
        hash.put(1, "sdaaaar");
        hash.put(1, "bjbh");

        System.out.println(hash.size());
        for (Object value : hash.values()) {
            System.out.println(value);
        }
    }
}

The output is -: 输出为-:

1
bjbh

Since the mapping for the key exist, it is replaced and the size remains 1 only. 由于键的映射存在,因此将其替换并且大小仅保留为1。

值被新键覆盖。大小保持不变,并且值被更改。这就是它的工作原理,因为键值始终是唯一的。您不能在一个键上映射多个值。

The API is the definitive reference and that is what you must believe. 该API是权威参考,这是您必须相信的。

A collision occurs when the hash of of a key already exists in the HashMap. 当HashMap中已经存在键的哈希时,就会发生冲突。 Then the values of the keys are compared, and if they are the different, the entries are placed in a linked list. 然后比较的值,如果的值不同,则将条目放置在链接列表中。 If the keys are the same, then the old key-value in the HashMap is overwritten. 如果键相同,则HashMap中的旧键值将被覆盖。

API documentation should normally be treated as authoritative unless there is very good reason to doubt its accuracy. API文档通常应视为权威文档,除非有充分的理由怀疑其准确性。

You should almost certainly ignore any claim that doesn't flag itself as 'knowingly' at odds with documentation and provide a testable evidence. 您几乎应该肯定会忽略任何没有与文档矛盾地标榜自己为“明知”的主张,并提供可验证的证据。

I humbly suggest you might be confused about the role of a linked 'collision' list. 我谦虚地建议您可能对链接的“冲突”列表的作用感到困惑。 As it happens HashMap in Java uses a linked-list to store multiple values for which the hash-code of the key is placed in the same 'bucket' as one or more other keys. 碰巧的是,Java中的HashMap使用链接列表来存储多个值,对于这些值,键的哈希码与一个或多个其他键放在同一“存储桶”中。

A HashMap in Java will always store a Key-Value-Pair. Java中的HashMap将始终存储键值对。 There are no linked lists involved. 没有涉及的链表。 What you are describing is the general idea of a hash map (often taught in computer science class), but the implementation in Java is different. 您所描述的是哈希映射的一般概念(通常在计算机科学课上讲授),但是Java中的实现是不同的。 Here, you will always have one value per key only (the last one you put in that place). 在这里,每个键始终只有一个值(放在该位置的最后一个)。

However, you are free to define a HashMap that contains List objects. 但是,您可以自由定义包含List对象的HashMap。 Though, you have to keep track of duplicates and collisions on your own then 但是,您必须自己跟踪重复和碰撞

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

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