HashMap";WriteOnce“;实施 import java.util.*; 公共类HashMapExample{ 公共静态类WriteOnceMap扩展HashMap{ 公共V输入(K键,V值){ /* WriteOnceMap是不允许更改特定键的值的映射。 这意味着,如果该键已经存在,put()方法应该抛出IllegalArgumentException 与地图中的某些值关联。 请实现此方法以符合上述WriteOnceMap的描述。 */ } public void putAll(Mappublic静态类WriteOnceMap扩展HashMap{ 公共V输入(K键,V值){ if(集装箱箱(钥匙)) 抛出新的IllegalArgumentException(键+“已在映射中”); 返回super.put(键、值); } 公共void putAll(Map

HashMap";WriteOnce“;实施 import java.util.*; 公共类HashMapExample{ 公共静态类WriteOnceMap扩展HashMap{ 公共V输入(K键,V值){ /* WriteOnceMap是不允许更改特定键的值的映射。 这意味着,如果该键已经存在,put()方法应该抛出IllegalArgumentException 与地图中的某些值关联。 请实现此方法以符合上述WriteOnceMap的描述。 */ } public void putAll(Mappublic静态类WriteOnceMap扩展HashMap{ 公共V输入(K键,V值){ if(集装箱箱(钥匙)) 抛出新的IllegalArgumentException(键+“已在映射中”); 返回super.put(键、值); } 公共void putAll(Map,java,hashmap,Java,Hashmap,1:Wrap HashMap) 2:或者使用:java.util.Collections.unmodifiableMap(Mapnice)作业,你尝试了什么?谢谢你的回答..你是个专家! import java.util.*; public class HashMapExample { public static class WriteOnceMap<K, V> extends HashMap<K, V> { public V put(K ke

1:Wrap HashMap)


2:或者使用:
java.util.Collections.unmodifiableMap(Mapnice)作业,你尝试了什么?谢谢你的回答..你是个专家!
import java.util.*;
public class HashMapExample {

    public static class WriteOnceMap<K, V> extends HashMap<K, V> {

        public V put(K key, V value) {
            /*
             WriteOnceMap is a map that does not allow changing value for a particular key.
             It means that put() method should throw IllegalArgumentException if the key is already
             assosiated with some value in the map.

             Please implement this method to conform to the above description of WriteOnceMap.
            */
        }


        public void putAll(Map<? extends K, ? extends V> m) {
            /*
             Pleaase implement this method to conform to the description of WriteOnceMap above.
             It should either
             (1) copy all of the mappings from the specified map to this map or
             (2) throw IllegalArgumentException and leave this map intact
             if the parameter already contains some keys from this map.
            */
        }
    }
}
public static class WriteOnceMap<K, V> extends HashMap<K, V> {
    public V put(K key, V value) {
        if (containsKey(key))
            throw new IllegalArgumentException(key + " already in map");

        return super.put(key, value);
    }


    public void putAll(Map<? extends K, ? extends V> m) {
        for (K key : m.keySet())
            if (containsKey(key))
                throw new IllegalArgumentException(key + " already in map");

        super.putAll(m);
    }
}