Collection Framework - Hashtable & Properties

Hashtable is a legacy class. It is based on hash table data structure itself.  The main difference between other classes and Hashtable is that it doesnt allow null values. It is synchronized and thread-safe.

Hashtable comes with the following constructors :

Hashtable() - creates a default hash table with initial capacity of 11 and load factor 0.75
Hashtable(int initialCapacity) - creates hash table with specified initial capacity and load factor 0.75
Hashtable(int initialCapacity, int loadFactor) - creates hash table with specified initial  capacity &                                                                                  load factor
Hashtable(Map m) - creates hash table from the specified map

The values in the table are stored based on the hashcodes of the keys.
If two keys have the same hash code, then the values are chained. Also if the hashtable has not reached its load factor, and we do not have a bucket for the hash code, then also the values are chained based on mode. e.g. if the hashcode is 16  and current buckets are 11, then the entry will  be stored in 16  mod 11 i.e. in the 5th bucket.

Image result for hashtable how values are stored

The example related to the hash table can be found here

Properties is another legacy class that stores key-value pairs. Generally, its used for storing configuration information of the application.

It comes with the following methods :

Properties()
load(inputStream)
getProperty(String propertyName)
setProperty(String name, String value)
propertyNames() 

Comments

Popular posts from this blog

Collection Framework - HashSet And LinkedHashSet class

Collection Framework - Cursors in Java

Hashed data structures