HashMap 无法添加到 Collections.singletonMap 中。

huangapple go评论81阅读模式
英文:

Hashmap is unable to be added to a collecitons.singletonmap

问题

import java.util.*;

public class Hash_Map_Demo {
    public static void main(String[] args) {
        Map<String, String> userEmails = new HashMap<>();
        userEmails.put("abc@gmail.com", "abc");

        Map<String, String> toEmails = Collections.singletonMap("key", "Value");
        toEmails.putAll(userEmails);
    }
}

运行上述代码时,我遇到了以下异常:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractMap.put(AbstractMap.java:209)
	at java.util.AbstractMap.putAll(AbstractMap.java:281)
	at Hash_Map_Demo.main(Hash_Map_Demo.java:13)

我理解在这里您试图将一个 HashMap 添加到 Collections.singletonmap 中。
但它们不是相同的,背后发生了什么。

英文:
import java.util.*;

public class Hash_Map_Demo {
    public static void main(String[] args) {
        Map&lt;String, String&gt; userEmails = new HashMap&lt;&gt;();
        userEmails.put(&quot;abc@gmail.com&quot;, &quot;abc&quot;);

        Map&lt;String, String&gt; toEmails = Collections.singletonMap(&quot;key&quot;, &quot;Value&quot;);
        toEmails.putAll(userEmails);
    }
}

When I'm trying to run the above code, I'm encountering the following exception:

Exception in thread &quot;main&quot; java.lang.UnsupportedOperationException
	at java.util.AbstractMap.put(AbstractMap.java:209)
	at java.util.AbstractMap.putAll(AbstractMap.java:281)
	at Hash_Map_Demo.main(Hash_Map_Demo.java:13)

I understand that here I'm trying to add a Hashmap to Collections.singletonmap.
But aren't they same and what is happening behind.

答案1

得分: 1

Collections.singletonMap() 方法返回的地图是不可变的,因此您不能在创建后修改它:

/**
 * 返回一个不可变映射,仅将指定的键映射到指定的值。返回的映射可序列化。
 *
 * @param &lt;K&gt; 映射键的类
 * @param &lt;V&gt; 映射值的类
 * @param key 要存储在返回映射中的唯一键。
 * @param value 返回的映射映射到{@code key}的值。
 * @return 仅包含指定键值映射的不可变映射。
 * @since 1.3
 */
public static &lt;K,V&gt; Map&lt;K,V&gt; singletonMap(K key, V value) {
    return new SingletonMap&lt;&gt;(key, value);
}
英文:

The map returned from Collections.singletonMap() is immutable so you cannot modify it after creation:

/**
 * Returns an immutable map, mapping only the specified key to the
 * specified value.  The returned map is serializable.
 *
 * @param &lt;K&gt; the class of the map keys
 * @param &lt;V&gt; the class of the map values
 * @param key the sole key to be stored in the returned map.
 * @param value the value to which the returned map maps {@code key}.
 * @return an immutable map containing only the specified key-value
 *         mapping.
 * @since 1.3
 */
public static &lt;K,V&gt; Map&lt;K,V&gt; singletonMap(K key, V value) {
    return new SingletonMap&lt;&gt;(key, value);
}

答案2

得分: 1

Collections.singletonMap() 返回一个不可变的映射。可以使用其他的 Map 实现,例如:

HashMap<String, String> toEmails = new HashMap();
toEmails.put("key", "Value")

Map<String, String> userEmails = new HashMap<>();
userEmails.put("abc@gmail.com", "abc");

toEmails.putAll(userEmails);
英文:

Collections.singletonMap() returns an immutable map. Use some other Map implementation, for example:

HashMap&lt;String, String&gt; toEmails = new HashMap();
toEmails.put(&quot;key&quot;, &quot;Value&quot;)

Map&lt;String, String&gt; userEmails = new HashMap&lt;&gt;();
userEmails.put(&quot;abc@gmail.com&quot;, &quot;abc&quot;);

toEmails.putAll(userEmails);

答案3

得分: 0

Collections.singletonMap() 返回一个 SingletonMap。这是 Map 的一种实现,但不支持 put 方法。当调用 SingletonMap.putAll 时,它会调用 SingletonMap.put,然后抛出异常。这就是为什么它是“不可变”的原因。

英文:

Collections.singletonMap() returns a SingletonMap. This is an implementation of Map but do not support put method. When the SingletonMap.putAll is called, it then calls SingletonMap.put and then throws an Exception. This is the reason why it's immutable.

huangapple
  • 本文由 发表于 2023年6月1日 15:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定