Followers

Wednesday, April 1, 2009

MAP Interface:A Map is an object that maps keys to values. Maps cannot contain duplicate keys: Each key can map to at most one value.

Map is not the child interface of Collection interface

If you want to store the values or objects as key value pairs we go for the the Map interface

Both key and values are objects

Duplication of keys is not allowed but values may be duplicated

Key –Value Pair is called an Entry

Methods in Map Interface:1)Object put(Object key,Object value) //Inserts a value into the map.if the key is already exists it will replace the old values with new one2)Object get(Object key) // Returns the value associated with the key otherwise returns null3)Object remove(Object key) // It removes the entry associated with the key and returns the corresponding value otherwise returns null4)boolean containsKey(Object key)5)boolean containsValue(Object key)6)int size()7)boolean isEmpty()8)void clean()9)void putAll(Map m)10)Set keySet()11)Collection values()12)Set emptySet()// The following three methods collection view of Map13)Object getKey();14)Object getValue();15)Object setValue(Object obj)//Entry is an inner interface present inside the map.It containsthe following three methods which can be applied on the entry object.16)Object getKey();17)Object getValue();18)Object setValue(Object obj)

LinkedHashMap:It is exactly similar to the HashMap except the following differencesHashMap:

Underlying datastructure is Hashtable

Insertion order is not preserved

While iterating we can not give guarantee for processing order. Hence we can not use it for caching

LinedHashMap:

Underlying datastructures are hashtable and doubly linkedlist

Insertion order of elements is preserved

While iterating elements we can give guarantee for processing order.Hence we can use for caching

IdentityHashMap:In case of HashMap JVM uses the equals() method to identify the duplicate keysBut if want to use the == operator to identify the duplicates we go for the IdentityHashMapIncase of IdentityHashMap two key reference i1 and i2 are equal if and only if bot i1 and i2 are pointing to the same object on the heap

HashMap dominates the garbage collector.if any objects are associated with the HashMap eventhough that object does not have any external references .Garbage collector is not allowed to destroy that object

But garbage collector dominate the WeakHashMap that is in case of hashmap if the key is not reachable garbage collector is allowed to destroy whole entry associated with the key