HashMap
HashMap available since Java1.2
HashMap extends AbstractMap<K,V> class.
HashMap implements following interfaces.
Serializable
Cloneable
Map
HashMap has four constructors
HashMap()
HashMap(int initialCapacity)
HashMap(int initialCapacity, float loadFactor)
HashMap(Map m)
Characteristics
If you create a HashMap using empty constructor then it will create hashMap with default capacity of 16 and loadfactor of 0.75.
Internally it will create and Array of Entry[CAPACITY] class.
Entry class contains key, value, next (object of Entry class) and hash member variables.
When you put any key,value pair into created hashMap first it will check for the null key. If null key is already available in the hashMap it will replace the value of the existing null key.
If the key is not null then it will calculate the hash value for the given key.
Then it will calculate index of the calcualted hash value and check if at that index entries already exist, then it will iterate over the available entries at that index and it put the new Entry pair at the end if the key is not equal to any available entries. But During the iteration on entries in the chain it check whether the hash value, and key value of the current pair is matching the incoming pair key, if both match then it will replace the existing value for the same key with the new coming value. If the key does not match with any key then it will add the new Entry pair at the last object's next reference.
Example
HashMap extends AbstractMap<K,V> class.
HashMap implements following interfaces.
Serializable
Cloneable
Map
HashMap has four constructors
HashMap()
HashMap(int initialCapacity)
HashMap(int initialCapacity, float loadFactor)
HashMap(Map m)
Characteristics
- Underlying data structure used it HashTable.
- It allows null values and null key.
- HashMap is unsynchronized.
- Odering not maintained.
- HashTable has two factors which can effects it performance, They are initialCapacity and loadfactor.
- InitialCapacity is the capacity of the HashMap when it is created.
- LoadFactor is about how full the HashMap is allowed before its capacity is automatically increased. It is called rehashing.
- Default loadFactor is 0.75
- Default initialCapacity is 16.
- HashMap allows duplate values.
- HashMap does not allow duplicate keys. Keys must be unique.
- The iterator returned by the HashMap respective methods is fail-fast.
- fail-fast : Assume the hashMap is being iterated by taken iterator, but outside the iterator loop if the hashMap is structurally modified by any other code then it will throw ConcurrentModificationException.
If you create a HashMap using empty constructor then it will create hashMap with default capacity of 16 and loadfactor of 0.75.
Internally it will create and Array of Entry[CAPACITY] class.
Entry class contains key, value, next (object of Entry class) and hash member variables.
When you put any key,value pair into created hashMap first it will check for the null key. If null key is already available in the hashMap it will replace the value of the existing null key.
If the key is not null then it will calculate the hash value for the given key.
Then it will calculate index of the calcualted hash value and check if at that index entries already exist, then it will iterate over the available entries at that index and it put the new Entry pair at the end if the key is not equal to any available entries. But During the iteration on entries in the chain it check whether the hash value, and key value of the current pair is matching the incoming pair key, if both match then it will replace the existing value for the same key with the new coming value. If the key does not match with any key then it will add the new Entry pair at the last object's next reference.
Example
import java.util.*; import java.util.Map.Entry; class HashMapFlight { public static void main(String args[]) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(100, "Sachin"); hashMap.put(101, "Dravid"); hashMap.put(102, "Kapil"); //Iterate using forEach for (Map.Entry<Integer,String> entry : hashMap.entrySet()) { System.out.println("Key: "+entry.getKey()+", Value: "+entry.getValue()); } //Iterate using Iterator Iterator<Entry<Integer, String>> iterator = hashMap.entrySet().iterator(); while(iterator.hasNext()) { Entry<Integer,String> entry = iterator.next(); System.out.println("Key: "+entry.getKey()+", Value: "+entry.getValue()); } //Iterating Keys Only Set<Integer> keys = hashMap.keySet(); for(Integer key : keys) { System.out.println("Key: "+key); } //Iterating Values only Collection<String> values = hashMap.values(); for(String value : values) { System.out.println("Value: "+value); } } }