ConcurrentHashMap program to make it unmodifiable in java


ConcurrentHashMap - making map unmodifiable using Collections.unmodifiableMap



HashMap and ConcurrentHashMap - Similarity and Differences


HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences


Map hierarchy in java - Detailed - HashMap, Hashtable, ConcurrentHashMap, LinkedHashMap, TreeMap, ConcurrentSkipListMap, IdentityHashMap



import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ConcurrentHashMapUnmodifiableExample {
   public static void main(String args[]){
         
          Map<Integer,String> concurrentHashMap=new ConcurrentHashMap<Integer,String>();
  
          concurrentHashMap.put(11, "ankit");
          concurrentHashMap.put(21, "mittal");
          concurrentHashMap.put(31, "javaMadeSoEasy");
         
          //getting unmodifiable ConcurrentHashMap
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(concurrentHashMap);
  
          /*
          * 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 ConcurrentHashMap_unmodifiable.ConcurrentHashMapUnmodifiableExample.main(ConcurrentHashMapUnmodifiableExample.java:24)
*/



RELATED LINKS>


HashMap - Iterate on keys by obtaining keySet, Iterate on values by obtaining values, Iterate on entry by obtaining entrySet


HashMap - synchronizing map using Collections.synchronizedMap



Labels: Core Java
eEdit
Must read for you :