■ Java HashMap - Complete Notes
■ What is HashMap?
- HashMap is part of Java's Collection Framework. - It stores data in key-value pairs. - Keys must
be unique. - Values can be duplicate. - Implements Map interface and uses a hash table internally.
■ Basic Syntax
HashMap mapName = new HashMap<>();
Example: HashMap map = new HashMap<>();
■ Common Operations
Operation Method Syntax Description
Add / Insert map.put(key, value); Adds or updates a key-value pair
Get value map.get(key); Returns value associated with key
Remove key map.remove(key); Removes key and its value
Contains Key map.containsKey(key); Checks if key exists
Contains Value map.containsValue(value); Checks if value exists
Size map.size(); Number of key-value pairs
Empty check map.isEmpty(); Checks if map is empty
Clear all map.clear(); Removes all key-value pairs
■ Example Code
import java.util.*; public class HashMapDemo { public static void main(String[] args) { HashMap
map = new HashMap<>(); map.put("apple", 2); map.put("banana", 5); map.put("orange", 3);
System.out.println("Bananas: " + map.get("banana")); map.put("banana", 10); if
(map.containsKey("apple")) { System.out.println("Apple is available"); } map.remove("orange"); for
(Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } }
■ Iteration Methods
1. Using entrySet()
for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue());
}
2. Using keySet()
for (String key : map.keySet()) { System.out.println(key + " = " + map.get(key)); }
3. Using forEach()
map.forEach((key, value) -> { System.out.println(key + " = " + value); });
■ Time Complexity
Operation Time Complexity
put(key, val) O(1)
get(key) O(1)
remove(key) O(1)
containsKey() O(1)
Iteration O(n)
■ When to Use HashMap
- When you need fast lookup by key - Counting frequency (characters, words, numbers) - Storing
data in key-value form - Caching or memoization - Grouping by key (e.g., anagrams)
■ Key Characteristics
- Allows null: One null key, multiple null values - Not synchronized (not thread-safe) - Order is not
guaranteed - Use LinkedHashMap to preserve order, TreeMap for sorted keys
■ Difference Between HashMap, LinkedHashMap, TreeMap
Feature HashMap LinkedHashMap TreeMap
Order No order Insertion order Sorted by key
Null Keys 1 allowed 1 allowed Not allowed
Speed Fastest Slightly slower Slower
Use Case General use Ordered output Sorted maps
■ Interview Tips
- Use HashMap for character frequency - Use HashMap for Two Sum, subarray problems - Watch
out for null keys - Know how to iterate using entrySet() - Understand hashing and collisions
(chaining, tree-bins)
■ Java 8+ Features
map.putIfAbsent("key", 1); map.computeIfPresent("key", (k, v) -> v + 1);
map.computeIfAbsent("newKey", k -> 100);
■ HashMap Limitations
- Not thread-safe - No ordering guarantee - Poor worst-case performance if many collisions
■ Summary Table
Feature Value
Implements Map<K, V>
Allows null key ■ One null key
Allows null values ■ Multiple allowed
Time complexity O(1) average
Thread-safe ■ No
Ordered ■ No