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.
*/
}
}
}
2
A
答えて
3
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);
}
}
+0
Xです。あなたは専門家です!助言のために – Mohsin
1
1:ラップのHashMap。
2:または使用:これは宿題であればjava.util.Collections.unmodifiableMap(Map<? extends K, ? extends V>)
dacweは、権利です。
+0
Xより。 – Mohsin
関連する問題
- 1. HashMap実装:--- hashcode
- 2. HashMap実装の概念
- 3. HashMapを実装する
- 4. Java HashMapの実装について
- 5. レイヤードHashMap N次HMMの実装
- 6. JAVA - 変更キー名機能を使用したHashMapの実装
- 7. ダイナミックプログラミングの実装が不良か、HashMapが遅いですか?
- 8. .Netでのワークフローの記述と実装
- 9. HashMapにはhash()という独自のhashCode()が実装されています。
- 10. Hashmap generic
- 11. HashMapのArrayListにエントリを追加する
- 12. HashMap Serializability
- 13. アプリケーションコンテキストのhashmap PHP
- 14. HashMapプロファイリング
- 15. XMLドキュメントインターフェイスの実装とクラスの実装
- 16. カスタムリストビューの実装
- 17. 実装とヘッダーファイルを追加する
- 18. javascript、.split()、追加変数を実装
- 19. 二重リンクリストの実装のためのaddメソッドの記述。オブジェクトを追加するときにソートする
- 20. ハッシュマップの実装方法
- 21. ReadDirectoryChangesWの実装
- 22. C++で "app.exe -instruction file"表記を実装する
- 23. .net:インターフェース実装を自動的に記入する
- 24. アンドロイドで記述子を実装できません
- 25. ビットトレントプロトコルの実装
- 26. ビジターカウンターの実装
- 27. ショッピングカートの実装
- 28. HashMapのパラメータ
- 29. ログウォッチャーの実装
- 30. リストのアレイベースの実装(カーソルの実装)
素敵な宿題、何を試しましたか?あなたの答えは – unbeli