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 */
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
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableMap.put(Unknown Source)
   at ConcurrentHashMap_unmodifiable.ConcurrentHashMapUnmodifiableExample.main(


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

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
//start paginationJs //end paginationJs