Advertisement
Google Ad Slot: content-top
Java Collection Map
A Map in Java is a key-value pair data structure where each key is unique and maps to a single value. Unlike other collections like List or Set, Map does not extend Collection since it works on key-value pairs.
Implementation |
Ordering |
Performance |
Null Keys/Values |
Use Case |
|---|---|---|---|---|
HashMap |
No order |
O(1) (Fastest) |
✅ 1 null key, multiple null values |
General-purpose, fastest |
LinkedHashMap |
Insertion Order |
O(1) |
✅ 1 null key, multiple null values |
Maintain insertion order |
TreeMap |
Sorted (Ascending by Key) |
O(log N) (Red-Black Tree) |
❌ No null keys, ✅ multiple null values |
Sorted data access |
HashMap:
LinkedHashMap:
TreeMap:
Iterating over a map:
List Methods:
Commonly Used Methods of List Interface
Method |
Description |
|---|---|
put(K key, V value) |
Adds a key-value pair |
get(K key) |
Retrieves value by key |
remove(K key) |
Removes an entry by key |
containsKey(K key) |
Checks if a key exists |
containsValue(V value) |
Checks if a value exists |
size() |
Returns number of key-value pairs |
keySet() |
Returns a |
values() |
Returns a |
entrySet() |
Returns a |