VOOZH about

URL: https://dev.to/harini_magesh_fa40041cf8d/map-in-java-5df6

⇱ Map in Java - DEV Community


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}