Menu

1 / 2

πŸ—ΊοΈ Maps Explained Like You're 5

DEV CommunityΒ·Sreekar ReddyΒ·6 months ago
#l8c7dufg
#eli5#datastructures#beginners#value#name
Reading 0:00
15s threshold

Key-value pairs for lookup

Day 136 of 149

πŸ‘‰ Full deep-dive with code examples

The Phone Book Analogy

Remember phone books?

Find "Smith" β†’ Flip directly to S section

Look up the name β†’ Get the phone number

You don't scan from the beginning!

Maps work like phone books!

You look up a "key" (name) and instantly get a "value" (phone number).

The Problem They Solve

Looking up things in a list is slow:

1 million items?

Check one by one?

That could take forever!

With a map:

Store key β†’ value pairs

Look up by key

Get the answer instantly!

How Maps Work

Maps store pairs of things:

Key β†’ Value "name" β†’ "Alice" "age" β†’ 30 "city" β†’ "Sydney"

Enter fullscreen mode

Exit fullscreen mode

To get a value, just use the key:

Ask for "name" β†’ Get "Alice"

Ask for "age" β†’ Get 30

No searching through everything!

Why They're Fast

Behind the scenes, maps use a smart trick:

Convert the key to a number (hashing)

Use that number to find the exact location

Go directly there

Like a library catalog card…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.