import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class HashMapTest {
public static void main(String args[]){
Map<Integer,String> hashMap=new HashMap<Integer,String>();
hashMap.put(11, "ankit");
hashMap.put(21, "mittal");
hashMap.put(31, "javaMadeSoEasy");
//getting unmodifiable HashMap
Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(hashMap);
/*
* Now any attempt to modify map will throw java.lang.UnsupportedOperationException
*/
unmodifiableMap.put(41,"java");
}
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableMap.put(Unknown Source)
at hashMap_unmodifiable.HashMapTest.main(hashMapTest.java:24)
*/
|
RELATED LINKS>