Java HashMap 允许重复吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20537898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:37:12  来源:igfitidea点击:

HashMap allows duplicates?

javahashmap

提问by Nani

I have a doubt regarding HashMap, as we all know HashMap allows one null key and value pair, My question here is

我对 HashMap 有疑问,众所周知,HashMap 允许一个空键和值对,我的问题是

If I wrote like this,

如果我这样写,

m.put(null,null);
m.put(null,a);

Will it throw a (error or exception) or will it override the value or what will be the value of returing??

它会抛出(错误或异常)还是会覆盖该值或返回的值是什么??

回答by Suresh Atta

Doesn't allow duplicates in the sense, It allow to add you but it does'nt care about this key already have a value or not. So at present for one keythere will be only one value

从某种意义上说不允许重复,它允许添加你,但它不关心这个键是否已经有一个值。所以目前一个键只有一个值

It silently overrides the valuefor nullkey. No exception.

它以静默方式覆盖valuefornull键。没有例外。

When you try to get, the last inserted value with nullwill be return.

当您尝试获取时,null将返回最后插入的值。

That is not only with nulland for any key.

这不仅适用null于任何键。

Have a quick example

有一个简单的例子

   Map m = new HashMap<String, String>();
   m.put("1", "a");
   m.put("1", "b");  //no exception
   System.out.println(m.get("1")); //b

回答by user2864740

Each keyin a HashMapmust be unique.

HashMap 中的每个都必须是唯一的。

When "adding a duplicate key" the old value (for the same key, as keys must be unique) is simply replaced; see HashMap.put:

当“添加重复键”时,旧值(对于相同的键,因为键必须是唯一的)被简单地替换;见HashMap.put

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.

Returns the previous value associated with key, or null if there was no mapping for key.

将指定值与此映射中的指定键相关联。如果映射先前包含键的映射,则旧值将被替换

返回与 key 关联的前一个值,如果没有 key 的映射,则返回null。

As far as nulls: a single null keyis allowed (as keys must be unique) but the HashMap can have any number of null values, and a null key need not have a null value. Per the documentation:

至于空值:允许单个空(因为键必须是唯一的)但 HashMap 可以具有任意数量的空,并且空键不需要具有空值。根据文档

[.. HashMap] permits null valuesand [a] null key.

[.. HashMap] 允许 null和 [a] null key

However, the documentation says nothingabout null/null needing to be a specific key/value pair or null/"a" being invalid.

但是,文档没有说明 null/null 需要是特定的键/值对或 null/"a" 无效。

回答by kai

Code example:

代码示例:

HashMap<Integer,String> h = new HashMap<Integer,String> ();

h.put(null,null);
h.put(null, "a");

System.out.println(h);

Output:

输出:

{null=a}

It overrides the value at key null.

它覆盖键 null 处的值

回答by user3094411

Hashmap type Overwrite that key if hashmap key is same key

哈希映射类型如果哈希映射键是相同的键,则覆盖该键

map.put("1","1111");
map.put("1","2222");

output

输出

key:value
1:2222

回答by dingjsh

HashMap don't allow duplicate keys,but since it's not thread safe,it might occur duplicate keys. eg:

HashMap 不允许重复键,但由于它不是线程安全的,因此可能会出现重复键。例如:

 while (true) {
            final HashMap<Object, Object> map = new HashMap<Object, Object>(2);
            map.put("runTimeType", 1);
            map.put("title", 2);
            map.put("params", 3);
            final AtomicInteger invokeCounter = new AtomicInteger();

            for (int i = 0; i < 100; i++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        map.put("formType", invokeCounter.incrementAndGet());
                    }
                }).start();
            }
            while (invokeCounter.intValue() != 100) {
                Thread.sleep(10);
            }
            if (map.size() > 4) {
// this means you insert two or more formType key to the map
               System.out.println( JSONObject.fromObject(map));
            }
        }

回答by BINIT kumar jha

m.put(null,null); // here key=null, value=null
m.put(null,a);    // here also key=null, and value=a

Duplicate keys are not allowed in hashmap.
However,value can be duplicated.

哈希图中不允许有重复的键。
但是,值可以复制。