Prev Next
- HashMap is a class implementing the Map interface, which allows us to store collection of object as key value pairs.
- It does not maintain the insertion order.
- It is not synchronized. It allows only one null key with null values.
Creating a HashMap:
Syntax:
HashMap obj = new HashMap(); //Without Generics |
Syntax:
HashMap<KeyType, ValueType> obj = new HashMap<KeyType, ValueType>(); //using Generics |
- By default a hashmap with a capacity of 16 is created and when the size of the hashmap (number of elements) exceeds 3/4th of the capacity (12), the capacity of the hashmap is doubled (32).
Example:
HashMap<Integer,String> Number = new HashMap<Integer,String>(); |
Syntax:
HashMap obj = new HashMap(int initialCapacity); |
- HashMap object ‘obj’ is created with the specified initial capacity. When the size of the hashmap exceeds 3/4th of the initial capacity, the capacity of the hashmap is doubled.
Example:
HashMap<Integer,String> Number = new HashMap<Integer,String>(12); |
HashMap object Number is created with initial capacity as 12. When 10th element is added, the capacity is doubled to 24.
Syntax:
HashMap obj = new HashMap(int initialCapacity, float loadFactor); |
- Creates an empty HashMap object with the specified initial capacity and load factor.
- Load factor is a value based on which the capacity of the hashmap is doubled.
- When the size of the hashmap reaches the value which is the product of initial capacity and the load factor, the capacity of hashmap is doubled
Example:
HashMap<Integer,String> Number = new HashMap<Integer,String>(12,0.5); |
- Hashmap object is created with an initial capacity of 12 and with load factor as 0.5. When the number of elements exceeds the product value (12*0.5 = 6), then the capacity will be doubled.
Methods of HashMap:
Methods of HashMap | Description |
void clear() | Removes all the key-value pairs from the Hashmap. |
boolean containsKey(Object key) | Returns true, if the hashmap object contains a value for the specified key. |
boolean containsValue(Object value) | Returns true, if the map contains a key for the specified value. |
Object get(Object key) | Returns the value to which the specified key is mapped, or null if the map contains no mapping for the key. |
Set entrySet() | Returns a Set which contains the key value pairs of the map. |
boolean equals(Object o) | Compares the specified object with the map object for equality. |
Value put(K key, V value) | Maps the specified key with the specified value in the map. |
int size() | Returns the number of key-value pairs in the map. |
Value remove(Object key) | Removes the key value pair for the specified key from the map if it is present. |
Collection values() | It returns the collection of values present in the map. |
Inserting the key-value pair:
Key value pairs can be mapped by using put() method.
Syntax:
put(K key, V value) where ‘K’ refers to the type of key and ‘V’ to the type of value. |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class HashMapExample{ public static void main(String[] args) { HashMap<Integer,String> stud = new HashMap<Integer,String>(); stud.put(100, "Anjali"); //adding key value pairs stud.put(101, "Anu"); stud.put(102, "Banu"); System.out.println(stud); // Prints the list of key,value pairs Iterator<Integer> it = stud.iterator(); //printing using Iterator while(it.hasNext()) { Integer key = it.next(); System.out.println("Key:"+key+"\t"+"value:"+stud.get(key)); } } } |
Output:
{100=Anjali, 101=Anu, 102=Banu}
Key:100 value:Anjali |
Removing the Mapping of a specified key value:
Association of the key to the value can be removed using the remove() method.
Syntax:
remove(Object key) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class HashMapExample{ public static void main(String[] args) { HashMap<Integer,String> stud = new HashMap<>(); stud.put(100, "Anjali"); stud.put(101, "Anu"); stud.put(102, "Banu"); System.out.println(stud); stud.remove(101); System.out.println("Mapping associated with the key 101 is removed.."); System.out.println(stud); } } |
Output:
{100=Anjali, 101=Anu, 102=Banu} Mapping associated with the key 101 is removed.. {100=Anjali, 102=Banu} |
Calculating length of the HashMap:
Length of the HashMap can be found using the size() method.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class HashMapExample{ public static void main(String[] args) { HashMap<Integer,String> stud = new HashMap<>(); stud.put(100, "Anjali"); stud.put(101, "Anu"); stud.put(102, "Banu"); System.out.println(stud); System.out.println("size of the student objects:"+stud.size()); } } |
Output:
{100=Anjali, 101=Anu, 102=Banu} size of the student objects:3 |