Map(Interface)
- Map is not child Interface of Collection.
- If we want to represent a group of object as Key-Value pairs, than we should go for Map.
- for eg:
- Both Keys & values are objects only.
- Duplicates keys are not allowed, But values can be duplicated.
- Each key-value pair is called Entry hence, Map is considered as a collection of Entry Objects.
Map(Interface) Method- Object put(Object key, Object value)
- To add one key value pair to the map.
- If the key already present than old value will be Replaced with New value and Return Old value.
- for eg:
m.put(101,"Shiva"); m.put(102,"Ravi");
m.put(101,"Raha");
output is 101-Raha,not Shiva because Old value replaced with new value.
2.void putAll(map m)
3.Object get(Object key)
- Returns the value associated with specified key.
4.Object remove(Object key)
- Removes the entry associated with specified key.
5.boolean containsKey(Object key)
6.boolean containsValue(Objectvalue)
7.boolean isEmpty()
8.int size()
9.void clear()
Another Method
- Set keySet()
- Collection values()
- Set entrySet()
These are Collection values of Map.
Entry(Interface)
- A map is a group of key-value pairs & each key-value pair is called an Entry. Hence, Map is considered as Entry Objects.
- Without Exiting Map object there is no chance of exiting Entry Object, Hence Entry(Interface) is defined inside Map Interface.
HashMap
- The underlying Data Structure is Hashtable.
- Insertion order is Not Preserved & it is based on HashCode of keys.
- Duplicates are Not allowed, but values can be Duplicated.
- Heterogeneous objects are allowed for both key & value.
- Null is allowed for key.[Only Once]
- Null is allowed for values.[Any Number of Times]
- HashMap implements Serializable, cloneable Interface but Not Random Access Interface.
- HashMap is best choice if our frequent operation is Search Operation.
Constructor
- HashMap m = new HashMap();
- Creates an Empty HashMap Object with default initial capacity 16 & default fill Ratio 0.75.
2.
HashMap m = new HashMap(int intialCapacity);
Creates an Empty HashMap Object with specified initial capacity & default fill ratio 0.75.
3.HashMap m = new HashMap(int initialCapacity, floatFillRatio);
4.HashMap m = new HashMap(map m);
Difference B/w HashMap & Hashtable
How to get Synchronized version of HashMap Object?
- By default HashMap is Non-Synchronized but we can get Synchronized version of HashMap by using Synchronized Map.
HashMap m = new HashMap(); map m1 = collections.synchronizedMap(m);
LinkedHashMap
- It is the child class of HashMap.
- It is exactly same as HashMap[Including Methods & Constructors] except the following differences.
Difference B/w HashMap & LinkedHashMap
Note:
- LinkedHashSet & LinkHashMap are commonly used for developing Cache based application.
IdentityHashMap
- It is exactly same as HashMap[Including Methods & Constructor] except the following difference.
- In the case of Normal HashMap [JVM will use .equals()] method to identify Duplicate keys which is meant for content comparison.
- But, In the case of IdentityHashMap JVM will use(==)operator to identify Duplicate keys, which is meant for difference Comparison.
Difference B/w (==)operator & .equals() methods
- In general(==)operator meant for Reference Content Comparison bracket Address Comparison.
- .equals() method meant for Content Comparison.
for eg:
Output is:
- I1 & I2 are Duplicate keys.
- I1.equals(I2) return True.
OutPut is:
- If we replace HashMap with IdentityHashMap than, I1 & I2 are not Duplicate keys. Because [I1==I2] returns False.
WeakHashMap
- It is exactly same as HashMap except the following difference.
Difference B/w WeakHashMap & HashMap- In the case of HashMap even though Object does not have any reference. It is not eligible for Garbage Collector. If it is associated with HashMap that is HashMap dominate Garbage collector.
- But In the case of WeakHashMap, If object does not contain any references it is eligible for Garbage Collector. Even object associated with WeakHashMap, that is Garbage Collector dominate WeakHashMap.
SortedMap
- It is child Interface of Map.
- If we want to represent a group of key-value pairs according to some sorting order of keys than we should go for Sorted Map.
- Sorting is based on key but not based on value.
SortedMap defines the following specific methods- Object firstKey();
- Object lastKey();
- SortedMap headMap(Object Key);
- SortedMap tailMap(Object Key);
- SortedMap subMap(Object Key1, Object Key2);
- Comparator comparator();
for eg:
- firstKey()-101
- lastKey() -135
- headMap(107)-{101-A,103-B,104-C}
- tailMap(107)-{107-D,125-E,135-F}
- subMap(103,125)-{103-B,104-C,107-D}
- comparator()-null
Thanks
Matri Sharma