What is HashMap in Java?
- A HashMap in Java is a part of the java.util package and is one of the foremost commonly used implementation of the Map interface.
- A HashMap stores information as a collection of key-value pairs, where each key is one of a kind and maps to completely one value.
- It allows capable retrieval, insertion, and deletion of elements based on keys.
- Not at all like a List, which stores elements in an ordered sequence, a HashMap organizes its components using a hash table, guaranteeing quick access times for key-based lookups.
- The HashMap usage gives constant-time performance (O(1)) for fundamental operations like get(), put(), and remove(), accepting the hash function distributes the keys consistently over the basic table.

Working of HashMap in Java
- The HashMap works based on a hash table, which is an array of spaces or buckets.
- Each space can hold distinctive entries, and these entries contain of a key-value pair.
- The key’s hash code is used to decide the record within the array (moreover known as a bucket).
- Here’s a step-by-step process of how a HashMap Works:
1.Hashing the Key:
- After you insert a key-value pair into a HashMap, the hashCode() method is called on the key to decide its hash value.
- The hash value is at that point mapped to a particular bucket or space within the fundamental array.
- In case two keys have the same hash code (a phenomenon called a hash collision), they will be stored within the same bucket or space using a linked list or tree structure.
2.Storing Entries:
- Once you insert an entry, the HashMap calculates the list for the bucket based on the key’s hash value.
- The put() method stores the key-value pair inside the suitable bucket.
- If the key as of presently exists, the value is updated; in case not, a new entry is made.
3.Handling Collisions:
- If two keys have the same hash value,a collision happens.
- In earlier versions of Java, collisions were taken care of by linking entries in a linked list.
- In more afterward versions (Java 8 and over), if the number of collisions gets to be too huge, the HashMap converts the linked list into a balanced tree (red-black tree), moving forward performance from O(n) to O(log n).
4.Resizing:
- As the number of entries in a HashMap increases, it may have to be resize its internal capacity to protect capability.
- When the number of entries surpasses a certain limit (frequently 75% of the current capacity), the HashMap is resized, and the entries are repeated and redistributed into a larger array.
5.Retrieving Values:
- To retrieve a value, the HashMap uses the get() method. It calculates the hash of the key, finds the comparing bucket, and looks for the key in that bucket.
- If the key is found, the related value is returned. In case not, null is returned.
Example of using HashMap in Java
Following is an example of how to use a HashMap in Java:
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
// Creating a HashMap to store employee details
Map<Integer, String> employeeMap = new HashMap<>();
// Adding key-value pairs to the HashMap
employeeMap.put(101, “Alice”);
employeeMap.put(102, “Bob”);
employeeMap.put(103, “Charlie”);
// Retrieving value using the key
System.out.println(“Employee with ID 102: ” + employeeMap.get(102)); // Output: Bob
// Checking if a key exists
if (employeeMap.containsKey(101)) {
System.out.println(“Employee ID 101 exists”);
}
// Removing an entry by key
employeeMap.remove(103);
System.out.println(“After removal, employee with ID 103: ” + employeeMap.get(103)); // Output: null
// Iterating over the entries of the HashMap
for (Map.Entry<Integer, String> entry : employeeMap.entrySet()) {
System.out.println(“ID: ” + entry.getKey() + “, Name: ” + entry.getValue());
}
}
}
Output:
Employee with ID 102: Bob
Employee ID 101 exists
After removal, employee with ID 103: null
ID: 101, Name: Alice
ID: 102, Name: Bob
Key Characteristics of HashMap
1.Unique Keys:
- A HashMap does not permit duplicate keys.
- In case a key as of now exists, the new value will overwrite the old value.
2.Null Keys and Values:
- A HashMap permits one null key and numerous null values.
- This is often different from a few other Map implementations, like Hashtable, which does not permit null keys or values.
3.Unordered:
- The entries in a HashMap are not ordered.
- The order of keys and values isn’t ensured to be the same as the order of insertion.
- In case you wish to preserve insertion order, you’ll be able use LinkedHashMap.
4.Thread-Unsafe:
- HashMap isn’t synchronized, meaning it isn’t thread-safe.
- On the off chance that different threads access a HashMap concurrently, it must be externally synchronized (using synchronizedMap or ConcurrentHashMap).
5.Performance:
- The performance of a HashMap is for the most part O(1) for fundamental operations like get(), put(), and remove() on average.
- In any case, within the case of hash collisions, the performance may degrade to O(n) for those operations in case numerous entries are put into the same bucket.
When to use HashMap?
- Once you got to store key-value pairs with fast lookups based on keys.
- When order of entries is not important.
- When thread-safety is not a concern, or once you can manage synchronization externally.