# HashMap (Condensed Guide)
## What Is HashMap?
`HashMap` is a **Map implementation** that stores **key-value pairs**.
It:
- Uses **keys to quickly retrieve values**
- Does **not maintain ordering**
- Allows **one null key**
- Allows **multiple null values**
```java
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> scores = new HashMap<>();Map
↓
HashMap
⚠ Map is not part of the Collection hierarchy, but it is part of the Java Collections Framework.
HashMap stores data using hashing.
Steps when storing data:
- The key’s
hashCode()is calculated - The hash determines a bucket location
- The value is stored at that location
Because of hashing:
- Lookups are extremely fast
- Keys must implement proper
equals()andhashCode()
| Operation | Complexity |
|---|---|
put() |
O(1) average |
get() |
O(1) average |
remove() |
O(1) average |
containsKey() |
O(1) |
Worst case (rare collisions) → O(n)
map.put("Alice", 90);
map.put("Bob", 85);map.get("Alice");map.remove("Bob");map.containsKey("Alice");map.size();for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}for (String key : map.keySet()) {
System.out.println(key + " -> " + map.get(key));
}- Expecting HashMap to maintain order
- Using mutable objects as keys
- Forgetting
equals()andhashCode()rules
| Feature | HashMap | LinkedHashMap | TreeMap |
|---|---|---|---|
| Order | None | Insertion order | Sorted |
| Speed | Fastest | Slightly slower | Slower |
| Use Case | General purpose | Maintain order | Sorted keys |
Use HashMap when you need:
- Fast key lookups
- Data associations
- Frequency counting
- Caching or indexing
Common examples:
- Word frequency counters
- User ID → profile lookup
- Student name → grade mapping
Map<String, Integer> wordCount = new HashMap<>();
wordCount.put("java", 3);
wordCount.put("python", 2);
System.out.println(wordCount.get("java"));Output:
3
- Count word frequencies in a sentence
- Implement a phone directory
- Find the first non-repeating character
- Solve the Two-Sum problem
HashMap = fast key-value storage using hashing.
Key characteristics:
- O(1) lookups
- No ordering
- Unique keys
Understanding HashMap is essential for solving many algorithm problems efficiently.