Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

Map in Java

DEV Community·Vidya·about 1 month ago
#zRk1tBIA
#java#beginners#programming#map#value#keys
Reading 0:00
15s threshold
Cover image for Map in Java

Vidya

What is Map in Java Collections?
A Map in Java (from Java Collections Framework) is a data structure that stores data in key–value pairs.

Each key is unique
Each value is mapped to a key
Think of it like a dictionary: word → meaning

Example:
101 → "John"
102 → "Alice"

Why do we use Map?
We use a Map when:

--> We need fast lookup using a key
--> We want to store pairs of related data
--> We need unique keys

Real-world examples:

Student ID → Name
Username → Password
Product ID → Price

Common Map Implementations
HashMap → Fast, unordered
LinkedHashMap → Maintains insertion order
TreeMap → Sorted by keys

Important Methods

put(key, value) → Insert data
get(key) → Get value
remove(key) → Delete
containsKey(key) → Check key
keySet() → Get all keys

Difference between Map vs List vs Set?

Read More