In today's insightful lesson, we will delve into a cornerstone of Java's Data Structure ecosystem, the HashMap. Building upon our understanding of HashSets from previous lessons, this session introduces you to HashMaps, a powerful structure that stores key-value pairs. This setup makes HashMap an ideal choice for swift data access through keys is necessary.
HashMaps utilize the principle of hashing, which enables constant time complexity for several core operations, thereby enhancing its efficiency. By the end of this lesson, you will have gained practical knowledge of creating, manipulating, and understanding the workings of HashMaps, including their implementation and complexity in handling data.
Before we commence, let's formally define a HashMap. A HashMap
in the world of Java, functions based on a hashtable, implementing the Map
interface. This interface implies that HashMaps can store key-value pairs, and interestingly, it allows null
values and a null
key. HashMaps do not guarantee any specific map order; in other words, the order can change over time.
HashMaps function using the principle of hashing. Here, a key is rendered to a hash code by a hash function, and this numeric code identifies the storage location for the key-value pair. Let's visualize a simple creation of a HashMap:
Java1import java.util.HashMap; 2 3class Solution { 4 5 public static void main(String[] args) { 6 // Creating the HashMap 7 HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); 8 9 // Adding key-value pairs to the HashMap 10 hashMap.put(1, "John"); 11 hashMap.put(2, "Mike"); 12 hashMap.put(3, "Emma"); 13 14 // Displaying the contents of the HashMap 15 System.out.println("HashMap: " + hashMap); // Outputs HashMap: {1=John, 2=Mike, 3=Emma} 16 } 17}
In the above code snippet, we have created a HashMap that maps an Integer key to a String value. Then, we add three key-value pairs and print the HashMap to the console.
In HashMaps, hashing takes center stage where the keys are hashed. Intriguingly, this hashed value helps us determine where to store the corresponding data.
This mechanism of hashing is what gives the HashMap its name. But the question that arises is, why is hashing important? Through hashing, it becomes possible to achieve constant time complexity, O(1)
, for get()
and put()
operations in ideal scenarios. This means that HashMaps provides extremely swift data access and insertion functionality — an advantage unrivaled by other data structures.
One thing to note is that due to the hashing mechanism, a HashMap might end up with multiple keys with the same hash code (known as a hash collision). To handle collisions, all keys with the same hash code are added to a linked list. Starting from Java 8, when this list becomes too large, it transforms into a balanced tree, enhancing worst-case performance from O(n) to O(log n).
HashMaps demonstrate an impressive O(1)
time complexity for basic operations — get()
and put()
. Derived from the concept of hashing, the key's hash code is used directly to store and retrieve elements, eliminating the need for scanning or searching. This gives HashMap a substantial edge in efficiency.
While it offers efficient time complexity operations, by using a HashMap, we need to be mindful of the space complexity as well. The space usage for HashMap can grow to O(n)
, where n is the number of elements in the HashMap.
We extend our earlier HashMap example to exhibit these operations:
Java1import java.util.HashMap; 2 3class Solution { 4 5 public static void main(String[] args) { 6 HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); 7 8 // Adding elements (put operation) 9 hashMap.put(1, "John"); 10 hashMap.put(2, "Mike"); 11 hashMap.put(3, "Emma"); 12 13 // Retrieving an element (get operation) 14 System.out.println("Element with key 1: " + hashMap.get(1)); 15 // Output: Element with key 1: John 16 17 // Removing an element (remove operation) 18 hashMap.remove(2); 19 20 System.out.println("HashMap after removal operation: " + hashMap); 21 // Output: HashMap after removal operation: {1=John, 3=Emma} 22 } 23}
Here, we use the get(key)
function to retrieve the value mapped to the provided key and the remove(key)
function to delete the designated key-value pair.
Throughout this lesson, you've deepened your understanding of HashMaps, exploring their structure and implementation. We've seen the utilization of hashing, which enables HashMaps to access elements with remarkable speed. By examining how HashMaps handles data and space complexity, you've laid a solid foundation for approaching large datasets.
As you progress further, applying your theoretical knowledge in practical settings is crucial. The upcoming exercises offer an opportunity to put your learnings to use, strengthen your understanding, and prepare you for tackling various coding problems and interviews with greater confidence. Let's get started!