0% found this document useful (0 votes)
15 views3 pages

Java HashMap Notes

Uploaded by

ashish123nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Java HashMap Notes

Uploaded by

ashish123nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ 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 [Link](key, value); Adds or updates a key-value pair


Get value [Link](key); Returns value associated with key
Remove key [Link](key); Removes key and its value
Contains Key [Link](key); Checks if key exists
Contains Value [Link](value); Checks if value exists
Size [Link](); Number of key-value pairs
Empty check [Link](); Checks if map is empty
Clear all [Link](); Removes all key-value pairs

■ Example Code
import [Link].*; public class HashMapDemo { public static void main(String[] args) { HashMap
map = new HashMap<>(); [Link]("apple", 2); [Link]("banana", 5); [Link]("orange", 3);
[Link]("Bananas: " + [Link]("banana")); [Link]("banana", 10); if
([Link]("apple")) { [Link]("Apple is available"); } [Link]("orange"); for
([Link] entry : [Link]()) { [Link]([Link]() + ": " + [Link]()); } } }

■ Iteration Methods
1. Using entrySet()
for ([Link] entry : [Link]()) { [Link]([Link]() + " = " + [Link]());
}
2. Using keySet()
for (String key : [Link]()) { [Link](key + " = " + [Link](key)); }
3. Using forEach()
[Link]((key, value) -> { [Link](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
[Link]("key", 1); [Link]("key", (k, v) -> v + 1);
[Link]("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

You might also like