TreeMap program to make it unmodifiable in java

TreeMap - 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.TreeMap;
import java.util.Map;
/** Copyright (c), AnkitMittal */
public class TreeMapUnmodifiableExample {
   public static void main(String args[]){
          Map<Integer,String> treeMap=new TreeMap<Integer,String>();
          treeMap.put(11, "ankit");
          treeMap.put(21, "mittal");
          treeMap.put(31, "javaMadeSoEasy");
          //getting unmodifiable TreeMap
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(treeMap);
          * 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 treeMap_unmodifiable.treeMapTest.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