• Articles
  • Tutorials
  • Interview Questions

HashMap in Java

HashMap in Java

Before diving into the world of HashMaps in Java, it is advisable to have a basic understanding of Java programming concepts, including variables, data types, loops, and conditional statements. A grasp of key data structures like arrays and lists will lay a solid foundation. Familiarity with object-oriented programming principles, such as classes and objects, will also be helpful. With these prerequisites, you’ll be well-equipped to explore the intricacies and unleash the power of HashMaps in Java.

To clear your basics with Java, watch

Video Thumbnail

What is a HashMap in Java?

In Java, a HashMap is a useful tool for storing and getting key-value pairs easily. Using hashing techniques allows fast access to data and performs important operations efficiently. To use HashMap effectively in Java applications, it’s important to understand its principles and functionality.

Now, let’s delve into them in greater detail.

  • A HashMap in Java is a robust data structure that efficiently stores and retrieves key-value pairs. 
  • Falling under the ‘Map’ interface in the Java Collections Framework, HashMap offers a dynamic and flexible means of organizing data.
  • The HashMap works on the principle of hashing, which involves converting the key into an index using a hash function. This index determines the bucket where the corresponding value will be stored. 
  • The key-value pairs are stored in an array of linked lists, known as buckets, which allow efficient storage and retrieval.
  • One of the key advantages of HashMap is its ability to provide constant-time performance for basic operations like insertion, deletion, and retrieval on average. 
  • To retrieve a value from a HashMap, a linear search or linked list traversal is performed within that bucket to find the desired value. 
  • The HashMap class also offers various methods to manipulate and access the data, such as put() to add a key-value pair, get() to retrieve the value associated with a key, remove() to delete a key-value pair, and containsKey() to check if a specific key exists.
  • It is important to note that HashMap does not guarantee the order of the elements. If you require a specific order, you can use the LinkedHashMap class instead.

An Example of Java HashMap 

An Example of Java HashMap

Imagine that, as a programmer, you are developing a simple contact management system and want to store your contacts’ names and phone numbers. Here’s how you can utilize a HashMap to accomplish this: 

Let’s dive into a practical example of using the HashMap class in Java to store and retrieve key-value pairs.

import java.util.HashMap;

public class Intellipaat {
    public static void main(String[] args) {
        // Create a new HashMap instance to store contacts
        HashMap<String, String> contacts = new HashMap<>();

        // Add contacts to the HashMap
        contacts.put("Ram", "+919999999999");
        contacts.put("Shyam", "+918888888888");
        contacts.put("Krishna", "+915555555555");

        // Retrieve and print the phone number of a specific contact        String ramsNumber = contacts.get("Ram");
        System.out.println("Ram's phone number: " + ramsNumber);

        // Check if a contact exists in the HashMap
        boolean hasshyam = contacts.containsKey("Shyam");
        System.out.println("Shyam exists in contacts: " + hasshyam);

        // Remove a contact from the HashMap
        contacts.remove("Krishna");

        // Print the updated contacts HashMap
        System.out.println("Updated contacts: " + contacts);
    }
}

Get 100% Hike!

Master Most in Demand Skills Now!

In the given illustration, we initiated the inclusion of the HashMap class from java.util package. Subsequently, we proceeded to instantiate a fresh HashMap object named “contacts,” wherein the key denotes the contact name (in the form of a String), while the value denotes the corresponding phone number (also in the form of a String).

We add contacts to HashMap using the put() method, specifying the key-value pairs. We can retrieve a specific contact’s phone number using the get() method, passing the corresponding name.

In order to verify the presence of a contact within the HashMap, one can utilize the containsKey() method by supplying the name as the input. Likewise, the removal of a contact from the HashMap can be accomplished through the use of the remove() method, with the name being specified as the parameter.

Finally, we print the updated contacts’ HashMap, showcasing the changes made.

Operations on HashMap

HashMap supports several operations that enable efficient data retrieval and manipulation. Let’s explore some of the key operations:

a) Insertion: To add elements to a HashMap, you can use the put(key, value) method. It associates the specified value with the specified key, allowing you to store and retrieve the value using the corresponding key.

Example:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 5);
map.put("orange", 8);

b) Accessing Elements: To retrieve elements from a HashMap, you can use the get(key) method. It returns the value associated with the specified key, or null if the key is not present in the HashMap.

Example:

Integer count = map.get("apple");
System.out.println(count);  // Output: 10

c) Removing Elements: The remove(key) method allows you to remove a key-value pair from the HashMap based on the specified key.

Example:

map.remove("banana");

d) Traversal of HashMap: For traversing or iterating over the elements in a HashMap, there are different approaches you can take. Here is a common method for traversing a HashMap in Java:

Using keySet() and forEach():

You can use the keySet() method to obtain a set of all the keys in the HashMap. Then, you can use the forEach() method to iterate over the keys and perform actions on the corresponding values.

Example: 

HashMap<String, Integer> map = new HashMap<>();

// Add elements to the HashMap
map.keySet().forEach(key -> {
    Integer value = map.get(key)
    // Perform actions on key and value
});

Internal Workings of HashMap

Understanding how HashMap works internally is crucial for effectively utilizing this data structure. Internally, a HashMap consists of an array of “buckets,” where each bucket can hold multiple key-value pairs. Here’s a simplified overview of the internal process:

a) Hashing: When you insert a key-value pair, the HashMap applies the hashCode() method on the key to generate a hash value. This hash value is then used to determine the index of the corresponding bucket in the underlying array.
b) Handling Collisions: In some cases, different keys can have the same hash value, resulting in a collision. To handle collisions, HashMap uses a linked list or a balanced tree (in Java 8+) within each bucket. It appends or balances the colliding key-value pairs in the same bucket.
c) Retrieving Elements: When you call the get(key) method, the HashMap calculates the hash value for the key and identifies the corresponding bucket. It then iterates through the linked list or searches the tree (if applicable) to find the desired key-value pair.
d) Performance Considerations: HashMap provides constant-time average performance for insertion, retrieval, and removal operations, provided the hash function and key distribution are optimal. However, poorly implemented hash functions or a high number of collisions can lead to performance degradation.

HashMap Methods in Java

Apart from the basic operations mentioned earlier, HashMap provides several other methods that offer flexibility and functionality. Here are some of the notable methods:

a) size(): Returns the number of key-value pairs in the HashMap.
b) containsKey(key): Checks if the HashMap contains the specified key.
c) containsValue(value): Checks if the HashMap contains the specified value.
d) keySet(): Returns a Set containing all the keys present in the HashMap.
e) values(): Returns a Collection containing all the values present in the HashMap.

Differences Between HashMap and HashSet

Differences Between HashMap and HashSet

The table below lists the differences between HashMaps and HashSets:

ParametersHashMapsHashSets
DuplicatesAllows duplicate values with distinct keysDoes not allow duplicate values
OrderingDoes not maintain any specific orderDoes not maintain any specific order
IterationIteration is based on keysIteration is based on elements
PerformanceProvides efficient constant-time operationsProvides efficient constant-time operations
ImplementationHashMaps use a hash table for storage and retrievalWhereas, HashSets use a hash table for storage and retrieval
Null ValuesHashMaps allow one null key and multiple null valuesAllows only one null value (no null keys)
UsageUseful when key-value mapping is requiredUseful when the uniqueness of elements is required
PurposeStores key-value pairsStores unique elements (values)

Types of HashMaps

Types of HashMaps

Mentioned below are the various types of HashMaps in Java:

  • LinkedHashMap: LinkedHashMap extends the functionality of HashMap by maintaining the insertion order of elements. It implies that when iterating through the elements, they are returned in the same order in which they were initially added. LinkedHashMap is useful when the order of insertion or access is important, such as when implementing a cache or maintaining a history.
  • TreeMap: TreeMap is a sorted implementation of the Map interface. It orders the elements based on their keys, allowing for efficient range queries and operations like finding the smallest or largest key. TreeMap is beneficial when you must maintain a specific order of elements or perform operations on a sorted dataset.
  • ConcurrentHashMap: ConcurrentHashMap is a thread-safe version of HashMap designed for concurrent access in multithreaded environments. It provides higher performance by allowing concurrent read operations without the need for synchronization while still ensuring the consistency and integrity of the data.
  • IdentityHashMap: IdentityHashMap uses reference equality (rather than object equality) to compare keys. It is useful when you want to treat different objects with the same content as distinct keys based on their memory addresses.
  • EnumMap: EnumMap is a specialized implementation of the Map interface that is used with enum keys. It offers high performance and type safety when working with enums as keys, thus providing efficient storage and retrieval.

Benefits of HashMaps in Java

Benefits of HashMaps in Java

Some of the benefits that HashMaps offer, which make them a robust data structure in Java, have been mentioned below:

  • Efficient Data Retrieval: HashMaps quickly retrieve values based on a unique key. With a constant time complexity of O(1) for retrieval operations, HashMaps excel in scenarios where quick access to data is crucial.
  • Flexible Key-Value Pairing: HashMaps allow the pairing of any key type with any value type, thereby offering flexibility in designing the data structures. This versatility enables the storage and retrieval of various data types, including custom objects.
  • Dynamic Size: HashMaps dynamically resize themselves to accommodate an arbitrary number of elements. As the number of elements increases, the HashMap automatically expands its internal capacity, optimizing memory usage.
  • Efficient Data Storage: HashMaps uses a hashing mechanism to distribute elements across the underlying data structure, thereby ensuring efficient storage and retrieval. This mechanism reduces the time required to search for a specific element compared to linear search algorithms.
  • Duplicate Key Handling: HashMaps automatically handle duplicate keys by replacing the existing value associated with the key. This feature simplifies scenarios where updating or replacing the existing data is necessary.
  • Enhanced Performance: HashMaps offer superior performance in scenarios that require frequent insertion, deletion, and lookup operations. Their efficient implementation results in faster execution and improved overall application performance.
  • Widely Used in the Java Collections Framework: HashMaps are a fundamental component of the Java Collections Framework, providing a widely adopted and standardized way to store and manage data. Their popularity and extensive usage ensure the availability of numerous resources, libraries, and community support.

Conclusion

In the blog, we concluded that HashMaps in Java are like magical containers that bring order and efficiency to the world of data storage. With their ability to quickly retrieve values based on unique keys, HashMaps empowers developers to organize and access information quickly. Whether you’re building a contact management system, implementing a cache, or solving complex data problems, harnessing the power of HashMaps unlocks a realm of possibilities. Embrace the art of efficient data storage and retrieval, and let HashMaps be your trusted companions on your Java coding journey.

Course Schedule

Name Date Details
Python Course 30 Nov 2024(Sat-Sun) Weekend Batch View Details
07 Dec 2024(Sat-Sun) Weekend Batch
14 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.