DEV Community

Harini
Harini

Posted on

Map in Java

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<>();
Enter fullscreen mode Exit fullscreen mode

Example

Map<Integer, String> students = new HashMap<>();
Enter fullscreen mode Exit fullscreen mode
  • 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

{101=Harini, 102=Rahul, 103=Kaviya}
Enter fullscreen mode Exit fullscreen mode

Important Methods in Map

1. put()

Used to insert key-value pairs.

map.put(101, "Harini");
Enter fullscreen mode Exit fullscreen mode

Example

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

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

System.out.println(map);
Enter fullscreen mode Exit fullscreen mode

Output

{1=Java, 2=Python}
Enter fullscreen mode Exit fullscreen mode

2. get()

Retrieves value using key.

System.out.println(map.get(1));
Enter fullscreen mode Exit fullscreen mode

Output

Java
Enter fullscreen mode Exit fullscreen mode

3. remove()

Removes entry based on key.

map.remove(1);
Enter fullscreen mode Exit fullscreen mode

Output

{2=Python}
Enter fullscreen mode Exit fullscreen mode

4. containsKey()

Checks whether a key exists.

System.out.println(map.containsKey(2));
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

5. containsValue()

Checks whether a value exists.

System.out.println(map.containsValue("Python"));
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

6. size()

Returns number of entries.

System.out.println(map.size());
Enter fullscreen mode Exit fullscreen mode

Output

2
Enter fullscreen mode Exit fullscreen mode

7. isEmpty()

Checks if map is empty.

System.out.println(map.isEmpty());
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

8. clear()

Removes all entries.

map.clear();
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
3
Enter fullscreen mode Exit fullscreen mode

Using values()

for(String value : map.values()) {
    System.out.println(value);
}
Enter fullscreen mode Exit fullscreen mode

Output

Java
Python
C++
Enter fullscreen mode Exit fullscreen mode

Using entrySet()

Most commonly used approach.

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

Output

1 : Java
2 : Python
3 : C++
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

{3=C, 1=A, 2=B}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

{1=A, 2=B, 3=C}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)