Java HashMap

Facebooktwitterredditpinterestlinkedinmailby feather

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one null key but many null values. It contains unique keys. It maintains no order. The initial capacity is 16, and the load factor is 0.75f. It can have multiple duplicate values.

Structure of HashMap

HashMap consist of the array of node and node is the class which contains 4 fields:

  1. Int Hash.
  2. K key.
  3. V value.
  4. Node next.

Declaration of HashMap

It uses the java.util. HashMap class. The declaration of HashMap is:

hm -> it holds the reference of the objects.

Constructors in HashMap:

  • HashMap (): This is the default constructor with initial capacity is 16, and the load factor is 0.75f.

Example:

Output:

  • HashMap (int capacity): It is used to create the HashMap with specified capacity, and load factor is 0.75.

Example:

Output:

  • HashMap (int capacity, float load factor): It is used to create the HashMap with specified capacity and load factor.

Example:

Output:

  • HashMap (Map map): It is used to create the HashMap by using the map object.

Example:

Output:

Time Complexity of HashMap:

The time complexity of HashMap operations is to provide Constant time. Iteration depends on the capacity of HashMap and the numbers of key-value pairs. It is directly proportional to Capacity + Size.

Methods of HashMap:

  1. clear (): It is used to remove all mapping from the map.

Example:

Output:

  1. get (Object key): It is used to get the value of a particular key from the map.

Example:

Output:

  1. put(object K, object V): It is used to insert the value of the specified key in the Map.

Example:

Output:

  1. containsKey(Object K): It returns true if the specified key is present on the map.

Example:

Output:

  1. containsValue (object V): It returns true if the specified value is present on the map.

Example:

Output:

  1. clone (): This is used to return the same copy of the map.

Example:

Output:

  1. isEmpty (): It is used to return true if the map is empty otherwise false.

Example:

Output:

  1. size (): This method is used to return the size of the map.

Example:

Output:

  1. putAll (Map m): This is used to copy all elements from the map to another map.

Example:

Output:

  1. remove (object K): This is used to remove the value of the particular key.

Example:

Output:

Difference between HashMap and HashTable:

HashMap HashTable
1. Traversed by Iterator. 1. Traversed by Enumerator and Iterator.
2. Inherits AbstractMap Class. 2. Inherits Dictionary Class.
3. Hashmap permits one null key and multiple null values. 3. hashTable do not allow one null key and value.
4. HashMap is not synchronized. 4. HashTable is synchronized.
5. Performance is fast. 5. Performance is slow.
6. Complexity is O (1). 6. Complexity is O (1).
7. Not legacy and introduced in 1.2v. 7. Legacy and introduced in 1.0v.

Traverse through a HashMap:

HashMap internally uses the Hashing technique. There are three ways to traverse in HashMap:

  1. using iterator:

Example:

Output:

  1. Using for-each loop:

Example:

Output:

  1. Using forEach () method:

Example:

Output:

Facebooktwitterredditpinterestlinkedinmailby feather