惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
G
Google Developers Blog
小众软件
小众软件
V
V2EX
月光博客
月光博客
腾讯CDC
aimingoo的专栏
aimingoo的专栏
J
Java Code Geeks
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
D
Docker
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Map in Java
Harini · 2026-06-16 · via DEV Community

Harini

What is Map in Java?

A Map in Java is a part of the Collections Framework that stores data in the form of key-value pairs.

  • Each key is unique.
  • Values can be duplicated.
  • A key is used to retrieve its corresponding value.
  • Map is available in the java.util package.
  • It does not extend the Collection interface.

Real-Time Example

Think of a student's record:

Roll Number (Key) Student Name (Value)
101 Harini
102 Rahul
103 Kaviya

Here:

  • Roll Number = Key
  • Student Name = Value

A key uniquely identifies a value.


Why Use Map?

Map is useful when you need:

  • Fast searching
  • Unique identifiers
  • Key-based data storage
  • Efficient lookup operations

Examples

  • Employee ID → Employee Details
  • Username → Password
  • Product ID → Product Information
  • Roll Number → Student Details
  • Country Code → Country Name

Syntax

Map<KeyType, ValueType> map = new HashMap<>();

Example

Map<Integer, String> students = new HashMap<>();

  • Integer → Key
  • String → Value

Creating a Map

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(101, "Harini");
        map.put(102, "Rahul");
        map.put(103, "Kaviya");

        System.out.println(map);
    }
}

Output

{101=Harini, 102=Rahul, 103=Kaviya}


Important Methods in Map

1. put()

Used to insert key-value pairs.

map.put(101, "Harini");

Example

Map<Integer,String> map = new HashMap<>();

map.put(1, "Java");
map.put(2, "Python");

System.out.println(map);

Output

{1=Java, 2=Python}


2. get()

Retrieves value using key.

System.out.println(map.get(1));

Output

Java


3. remove()

Removes entry based on key.

map.remove(1);

Output

{2=Python}


4. containsKey()

Checks whether a key exists.

System.out.println(map.containsKey(2));

Output

true


5. containsValue()

Checks whether a value exists.

System.out.println(map.containsValue("Python"));

Output

true


6. size()

Returns number of entries.

System.out.println(map.size());

Output

2


7. isEmpty()

Checks if map is empty.

System.out.println(map.isEmpty());

Output

false


8. clear()

Removes all entries.

map.clear();


Traversing a Map

Using keySet()

Map<Integer,String> map = new HashMap<>();

map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");

for(Integer key : map.keySet()) {
    System.out.println(key);
}

Output

1
2
3


Using values()

for(String value : map.values()) {
    System.out.println(value);
}

Output

Java
Python
C++


Using entrySet()

Most commonly used approach.

for(Map.Entry<Integer,String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " : " + entry.getValue());
}

Output

1 : Java
2 : Python
3 : C++


Implementations of Map Interface

1. HashMap

Features

  • Most commonly used
  • No insertion order maintained
  • Allows one null key
  • Allows multiple null values
  • Fast performance

Example

Map<Integer,String> map = new HashMap<>();

map.put(101, "Harini");
map.put(102, "Kaviya");

System.out.println(map);


2. LinkedHashMap

Features

  • Maintains insertion order
  • Slightly slower than HashMap
  • Allows null key and values

Example

Map<Integer,String> map = new LinkedHashMap<>();

map.put(3, "C");
map.put(1, "A");
map.put(2, "B");

System.out.println(map);

Output

{3=C, 1=A, 2=B}


3. TreeMap

Features

  • Stores keys in sorted order
  • Does not allow null keys
  • Based on Red-Black Tree

Example

Map<Integer,String> map = new TreeMap<>();

map.put(3, "C");
map.put(1, "A");
map.put(2, "B");

System.out.println(map);

Output

{1=A, 2=B, 3=C}