Map in Java

Published: (May 2, 2026 at 12:49 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Cover image for Map in Java

What is Map in Java Collections?

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

  • Each key is unique.
  • Each value is associated with 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) – Retrieve the value for a key.
  • remove(key) – Delete the entry for a key.
  • containsKey(key) – Check if a key exists.
  • keySet() – Get a set of all keys.

Difference between Map vs List vs Set?

Difference between Map, List, and Set

0 views
Back to Blog

Related posts

Read more »

MVC Architecture Flow in Spring Boot

!Cover image for MVC Architecture Flow in Spring Boothttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2F...