Download - Reference Guide to Java - Day4
1
Map Interface
1
Map Interface It is introduced in 1.2 version of java.
It is based on key-value paired techniques(Hashing
techniques).
It represents the three collection view one as a set of key,one
as collection of values, or set of key-value mappings.
1
Map Interface We can say that it replace the deprecated abstract class
Dictionary .
A map elements can't contains duplicate keys but can
contain duplicate values.
It is non-synchronized.
1
HashMap It is used from 1.2 version of java.
It is based on key-value paired techniques(Hashing
techniques).
It extends AbstractMap class and implements Cloneable and
Serializable interface.
It Doesn't have any fix order for generating result ,even it
does not guarantee that the order will remain constant over
time.
1
HashMap It allow null key and value but can have only one null key
and multiple null values.
Iteration cant be performed directly on Map elements.It is
performed by generating collection view of Map.
HashMap class i.e nearly equivalent to Hashtable class by
feature.
1
Hashtable It is used from Jdk1.0.
A Hashtable is an array of list.
Each list of array in hashtable known as a bucket.
Bucket position is identified by calling the method
hashcode( ).
A Hashtable contains values based on the key means it is
based on key-value pair.
1
Hashtable It implements the Map interface and extends Dictionary
class.
It contains only unique elements.
It doesn’t have any null key or null value.
It is by default synchronized .
An instance of Hashtable has two parameters initial capacity
and load factor that affect its performance.
1
Hashtable To successfully store and retrieve objects from a hashtable,
the objects used as keys must implement the hashCode and
equals method.
Simple capacity is the number of buckets in the hash table,
and the initial capacity is simply the capacity at the time the
hash table is created. Note that the hash table is open in
case of a "hash collision", a single bucket stores multiple
entries, which must be searched sequentially.
1
Hashtable The load factor is a measure of how full the hashtable is
allowed to get before its capacity is automatically increased.
The initial capacity and load factor parameters are merely
hints to the implementation. The exact details as to when
and whether the rehash method is invoked are
implementation-dependent.
1
Hashtable Generally, the default load factor (.75) offers a good tradeoff
between time and space costs. Higher values decrease the
space overhead but increase the time cost to look up an
entry (which is reflected in most Hashtable operations,
including get and put ).
The initial capacity controls a tradeoff between wasted
space and the need for rehash operations, which are time-
consuming.
1
Hashtable No rehash operations will ever occur if the initial capacity
is greater than the maximum number of entries the
Hashtable will contain divided by its load factor. However,
setting the initial capacity too high can waste space.
If many entries are to be made into a Hashtable ,creating it
with a sufficiently large capacity may allow the entries to be
inserted more efficiently than letting it perform automatic
rehashing as needed to grow the table.
1
HashMap vs Hashtable HashMap is not synchronized where as Hashtable
is synchronized.
HashMap allow null key and null values where as Hashtable
doesn't allow null key as well as null value,it will generate
NullPoniterException when null is added.
Only one thread can take control over this if multiple
threads want to take control over it then they have to
maintain the queue that's why it is known as thread-safe.
Thanks
facebook.com/apex.tgi
twitter.com/ApextgiNoida
pinterest.com/apextgi
Stay Connected with us for more chapters on JAVA