英文:
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<String, String> userEmails = new HashMap<>();
userEmails.put("abc@gmail.com", "abc");
Map<String, String> toEmails = Collections.singletonMap("key", "Value");
toEmails.putAll(userEmails);
}
}
When I'm trying to run the above code, I'm encountering the following exception:
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)
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 <K> 映射键的类
* @param <V> 映射值的类
* @param key 要存储在返回映射中的唯一键。
* @param value 返回的映射映射到{@code key}的值。
* @return 仅包含指定键值映射的不可变映射。
* @since 1.3
*/
public static <K,V> Map<K,V> singletonMap(K key, V value) {
return new SingletonMap<>(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 <K> the class of the map keys
* @param <V> 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 <K,V> Map<K,V> singletonMap(K key, V value) {
return new SingletonMap<>(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<String, String> toEmails = new HashMap();
toEmails.put("key", "Value")
Map<String, String> userEmails = new HashMap<>();
userEmails.put("abc@gmail.com", "abc");
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论