generated from cotes2020/chirpy-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e608b7f
commit 5fab8ad
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Demystifying HashMap - A Look Under the Hood | ||
date: 2024-04-30 12:29:00 +0800 | ||
categories: [Tech] | ||
tags: [Data Structure] | ||
render_with_liquid: false | ||
--- | ||
|
||
HashMap is a fundamental data structure in the Java world, widely used for its efficient key-value pair storage and retrieval. But have you ever wondered how it all works behind the scenes? In this blog, we'll delve into the implementation details of HashMap, exploring its core components and operations. | ||
|
||
### Understanding the Basics: Keys, Values, and Hashing | ||
|
||
At its heart, HashMap stores key-value pairs. Keys act as unique identifiers for accessing the corresponding values. However, efficiently finding a specific value based on its key is crucial. This is where hashing comes in. The `hashCode()` method plays a vital role. It transforms each key into a unique integer (hash code). This hash code is then used to map the key-value pair to a specific bucket within an internal array. | ||
|
||
### The Inner Workings: Buckets and Collision Resolution | ||
|
||
HashMap utilizes an internal array of buckets. Ideally, each key would have its own dedicated bucket. However, in reality, collisions occur when multiple keys hash to the same value. To resolve collisions, HashMap employs various techniques. One common approach is chaining. In chaining, collided key-value pairs are linked together in a separate data structure (like a linked list) attached to the bucket. | ||
|
||
### Putting It All Together: Key Operations | ||
|
||
Now, let's explore some key HashMap operations: | ||
|
||
* **put(key, value):** This method adds a new key-value pair to the HashMap. It first calculates the hash code for the key. Then, it finds the corresponding bucket and inserts the key-value pair. If a collision occurs, the entry is chained to the existing entries in that bucket. | ||
* **get(key):** This method retrieves the value associated with a specific key. It calculates the hash code for the key and locates the corresponding bucket. Then, it iterates through the chain of entries (if any) searching for the matching key. If found, the value is returned. | ||
* **remove(key):** This method removes a key-value pair from the HashMap. Similar to `get`, it finds the bucket using the key's hash code. Then, it iterates through the chain searching for the key. Once found, the entry is removed from the chain. | ||
|
||
### Performance Considerations: Load Factor and Rehashing | ||
|
||
HashMap's performance heavily relies on a concept called load factor. It represents the ratio of key-value pairs to the number of buckets. As the load factor increases (more entries added), collisions become more frequent, impacting performance. To maintain efficiency, HashMap utilizes a technique called rehashing. When the load factor reaches a predefined threshold, the HashMap automatically resizes its internal bucket array and redistributes the entries. This minimizes collisions and ensures optimal performance. | ||
|
||
### Beyond the Basics: Exploring HashMap's Advancements | ||
|
||
Java HashMap offers additional features to cater to specific use cases: | ||
|
||
* **containsKey(key):** This method checks if a specific key exists in the HashMap without retrieving the value. | ||
* **entrySet():** This method returns a set of all key-value pairs in the HashMap. | ||
* **Iterators:** HashMap provides iterators to traverse over keys or key-value pairs. | ||
* **Custom Key Classes:** You can create your own key classes as long as they implement the `hashCode()` and `equals()` methods correctly. | ||
|
||
### Conclusion: HashMap - A Powerful and Versatile Tool | ||
|
||
By understanding the internal workings of HashMap, you gain valuable insight into its strengths and potential trade-offs. HashMap offers a powerful and versatile data structure for storing and retrieving key-value pairs efficiently. By leveraging its functionalities effectively, you can optimize your Java applications and streamline data management tasks. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters