Hashtable Immutability: to make the table read-only using Collections.unmodifiableMap(Map map) : Hashtable Basics « Collections « Java Tutorial






import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;

public class MainClass {
  public static void main(String[] s) {
    Hashtable table = new Hashtable();
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "value3");

    Map m = Collections.unmodifiableMap(table);

    m.put("key3", "value3");

    System.out.println(m);

  }
}
Exception in thread "main" java.lang.UnsupportedOperationException
  at java.util.Collections$UnmodifiableMap.put(Collections.java:1286)
  at MainClass.main(MainClass.java:14)








9.48.Hashtable Basics
9.48.1.Creating Hash Tables
9.48.2.Adding Key-Value Pairs
9.48.3.Get all keys and elements from a hash table
9.48.4.Copying all the key-value pairs from one Hashtable (or any Map) into another Hashtable: the putAll() method
9.48.5.Displaying Hash Table Contents
9.48.6.Removing Key-Value Pairs: call the remove() method with the specific key as its argument
9.48.7.To get rid of all key-value pairs: call the clear() method
9.48.8.Sizing Hash Tables
9.48.9.Fetching Keys and Values: the get() method
9.48.10.To perform some operation on all keys: the keys() or keySet() methods
9.48.11.Using the elements() or the values() method to get the set of all the values in the hash table
9.48.12.Getting elements (key-value pairs) from a Hashtable: the entrySet() method
9.48.13.Finding Elements
9.48.14.contains() and containsValue(): check to see if a specific value is found within the Hashtable
9.48.15.Cloning Hash Tables
9.48.16.Hashtable Immutability: to make the table read-only using Collections.unmodifiableMap(Map map)
9.48.17.Counting Word Occurrences with a hash table
9.48.18.uses a generic Hashtable to store the names of bank depositors and their current balances
9.48.19.Check if a particular key exists in Java Hashtable
9.48.20.Check if a particular value exists in Java Hashtable
9.48.21.Iterate through keys of Java Hashtable
9.48.22.Iterate through values of Java Hashtable
9.48.23.Remove all values from Java Hashtable
9.48.24.Remove value from Java Hashtable
9.48.25.Get Collection of Values from Hashtable
9.48.26.Get Set view of Keys from Hashtable
9.48.27.Scan the content of a hashtable
9.48.28.Sort keys in an Hashtable
9.48.29.Use iterators with a Hashtable.