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의 구현 문제
- 2. HashMap의 List에 추가하기위한 단축키
- 3. HashMap의 성능과 우위
- 4. 는 HashMap의 문제 (자바)
- 5. 문자열은 HashMap의 키로 사용
- 6. HashMap의 오류 : 다음과 같이 javax.el.PropertyNotFoundException
- 7. Java에서 HashMap의 최대 크기 제한
- 8. Tomcat6 - 2 회 전쟁, 2 회
- 9. 회 전자 - 클릭하여 회 전자에 무언가를 추가하십시오.
- 10. 아약스 회
- 11. 회 전자
- 12. 나는 프로그래밍 방식으로 인해 우리는 생산의 아래처럼 로그를 받고있는 HashMap의 객체에 비동기 액세스로 인해 HashMap의
- 13. alt-rt.jar와 rt.jar에서 HashMap의 차이점은 무엇입니까?
- 14. HashMap의 빈 인덱스 안에 무엇이 있습니까?
- 15. 초기 용량과 부하율이 다른 HashMap의 성능
- 16. 자바 : HashMap의 <String, int>
- 17. Android : 다른 회 전자의 옵션을 선택한 후 회 전자 만들기
- 18. 독립적 인 회 전자에서 종속 회 전자 채우기
- 19. 회 전자 1 선택에 따라 회 전자 2를 업데이트하는 방법
- 20. 회 전자 이벤트보기의 이름
- 21. 회 전자 작은 크기
- 22. 회 전자와 대화 숨기기
- 23. 회 전자/줄임표/너비
- 24. 안드로이드, SQLite는, 회 전자 :
- 25. 맞춤 회 전자 팝업
- 26. ArrayAdapter - 회 전자 만들기?
- 27. Webview Xcode의 회 전자로드
- 28. 스칼라 스핀의 회 전자
- 29. 회 전자에 알파벳을 표시합니다.
- 30. 루프가 8 회
좋은 숙제, 무엇을 시도 했습니까? 당신의 대답은 – unbeli