HashMap in Java
In the last blog, we learned about the NavigableMap interface. To know more about the NavigableMap interface, visit NavigableMap Interface in Java. In this blog, we will learn about the class called HashMap.
HashMap
HashMap class is a class that provides the basic implementation of the Map interface. Like in Map, we use key-value pairs to store the data. The same approach is used in HashMap too. One object is used as a key to other objects. The key in the HashMap should be unique. If we try to use a duplicate key, it will replace the corresponding key element. HashMap is similar to HashTable but the difference is that the HapMap is not synchronized. HashMap allows a null key as well but there should be only one null key and there can be multiple null values. In order to use the HashMap we have to import the package called java.util. Let's see the example:
e.g.
Output
HashMap Constructors
- HashMap() - HashMap() is used to construct a default HashMap.
- HashMap(int initialCapacity) - HashMap(int initialCapacity) is used to initialize the capacity of the hash map to the given integer value, capacity.
- HashMap(int capacity, float loadFactor) - HashMap(int capacity, float loadFactor) is used to initialize both the capacity and load factor of the hash map with the help of its arguments.
- HashMap(Map <? extends Key,? extends Value> m) - HashMap(Map <? extends Key,? extends Value> m) is used to initialize the hash map by using the elements of the given Map object m.
e.g.
Methods of HashMap
- clear()
- clone()
- isEmpty()
- size()
- put()
- putAll()
- putIfAbsent()
- remove()
- containsKey()
- containsValue()
- replace()
- get()
- keySet()
- values()
clear() method removes all the key-value pairs of the hashmap.
e.g.
Output
clone() method is used to return a shallow copy of the HashMap instance. A shallow copy means references of key and value are copied not key and value.
e.g.
Output
isEmpty() method checks whether the hashmap is empty or not.
e.g
Output
size() method returns the number of entries in the hashmap.
e.g.
Output
put() method adds or insert the elements in the Hashmap.
e.g.
e.g.
putAll() method adds or inserts all the key-value mappings from the specified Map to the HashMap.
e.g.
Output
putIfAbsent() method inserts the specified value with the specified key in the map only if it is not already present in the hashmap.
e.g.
Output
remove() method removes or deletes the specified values with the associated specified keys from the map.
e.g.
Output
The containsKey() method determines whether the mapping for the supplied key exists in the hashmap. If the key is missing, it returns false.
e.g.
Output
containsValue() method checks if the specified value is present in one or more mappings of the hashmap or not. If the value is not present then it returns false.
e.g.
Output
replace() method replaces the old value with the new value for a specified key.
e.g.
Output
get() method returns the value of the specified key in the hashmap.
e.g.
Output
The keySet() method is used to return a set view of the keys present in this map.
e.g.
Output
values() method returns the view of all the values present in the entries of the hashmap.
e.g.
Output