Map is a collection of key and value object associations. You can do all basic operations as in Lists and Sets such as adding , removing ,and accessing key-value pairs. There are four types of Map implementations they are HashMap, Hashtable, WeakHashMap and TreeMap.
HashMap, Hashtable and WeakHashMap have similar implementations. TreeMap is meant for sorted collection, this can be filtered down based on the requirement. Then we have other three types to choose from. The choice again depends upon your requirement. Your requirement could be
1.Thread safe
2.Type of operation ( basic operations )
HashMap and WeakHashMap are not synchronized whereas Hashtable is synchronized.
WeakHashMap is a special purpose map which uses an internal hashtable. When there are no more references to key object except weak reference maintained by WeakHashMap, the garbage collector reclaims the key object and mapping between key object and value object is also reclaimed, if the value object does not have any other references then the value object is also reclaimed by the garbage collector.
If you want your Map type collection to be thread safe, then you need to use Hashtable otherwise use HashMap. HashMap gives better performance than Hashtable because of it's non-synchronized methods. The reason I did not give any bench marks for Map types is that it is pretty straight forward to choose Map type depending on requirement .
You can improve performance by using proper initial size and load factor in the constructor for all types of Map types except TreeMap.
The constructors are
HashMap(int initialcapacity)
HashMap(int initialcapacity, float loadfactor)
Hashtable(int initialcapacity)
Hashtable(int initialcapacity, float loadfactor)
WeakHashMap(int initialcapacity)
WeakHashMap(int initialcapacity, float loadfactor)
When the number of objects exceed loadfactor capacity, then the capacity of the class increases to (2*capacity + 1). The default load factor is 0.75.
HashMap, Hashtable and WeakHashMap have similar implementations. TreeMap is meant for sorted collection, this can be filtered down based on the requirement. Then we have other three types to choose from. The choice again depends upon your requirement. Your requirement could be
1.Thread safe
2.Type of operation ( basic operations )
HashMap and WeakHashMap are not synchronized whereas Hashtable is synchronized.
WeakHashMap is a special purpose map which uses an internal hashtable. When there are no more references to key object except weak reference maintained by WeakHashMap, the garbage collector reclaims the key object and mapping between key object and value object is also reclaimed, if the value object does not have any other references then the value object is also reclaimed by the garbage collector.
If you want your Map type collection to be thread safe, then you need to use Hashtable otherwise use HashMap. HashMap gives better performance than Hashtable because of it's non-synchronized methods. The reason I did not give any bench marks for Map types is that it is pretty straight forward to choose Map type depending on requirement .
You can improve performance by using proper initial size and load factor in the constructor for all types of Map types except TreeMap.
The constructors are
HashMap(int initialcapacity)
HashMap(int initialcapacity, float loadfactor)
Hashtable(int initialcapacity)
Hashtable(int initialcapacity, float loadfactor)
WeakHashMap(int initialcapacity)
WeakHashMap(int initialcapacity, float loadfactor)
When the number of objects exceed loadfactor capacity, then the capacity of the class increases to (2*capacity + 1). The default load factor is 0.75.
All these classes work in accordance with hash values which are used to identify the value objects. The key objects are converted to integer called hash code by using hashing algorithms which is used as an index for value objects. Hash code must be same for two equal objects, i.e. when tested with the equals() method,it must return true. The hash code is determined by using hashCode() method. The hash code of a collection is determined by cumulating all the hash codes of the associated objects.
Key Points :
- Use HashMap for non-thread safe map collection otherwise use Hashtable for thread safe collection.
- Use TreeMap for non-thread safe ordered map collection otherwise use synchronized TreeMap for thread safe.
No comments:
Post a Comment