LinkedHashMap - making it unmodifiable in java


LinkedHashMap - making map unmodifiable using Collections.unmodifiableMap


HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences


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


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



No comments:

Post a Comment