• March 26, 2025

Collection vs Map: What is Difference?

In Java, Collection and Map are two key parts of the Java Collection Framework (JCF), but they have distinct roles:

  • Collection is a root interface that represents a group of objects (like List, Set, Queue).
  • Map is a separate interface used for key-value pair storage.

1. What is Collection?

Collection is the root interface for all collections that store a group of objects in Java.

Features of Collection Interface:

✔ Represents a group of elements (single values).
✔ Extended by List, Set, and Queue.
✔ Provides methods like add(), remove(), size(), contains().
✔ Does not store key-value pairs (only elements).
✔ Cannot be instantiated directly (must use classes like ArrayList or HashSet).

Collection Hierarchy

plaintextCopy codeCollection (Interface)
├── List (Interface) → ArrayList, LinkedList, Vector
├── Set (Interface) → HashSet, TreeSet, LinkedHashSet
└── Queue (Interface) → PriorityQueue, Deque

Example Using Collection Interface

import java.util.*;

public class CollectionExample {
public static void main(String[] args) {
Collection<String> names = new ArrayList<>(); // Collection reference
names.add("Sujit");
names.add("Patel");

System.out.println(names); // Output: [Sujit, Patel]
}
}

🔹 Here, Collection is used to store individual elements.


2. What is Map?

Map is an interface that stores key-value pairs (not a subtype of Collection).

Features of Map Interface:

✔ Stores key-value pairs (key → value).
✔ Keys are unique (no duplicates), but values can be duplicated.
✔ Provides methods like put(), get(), remove(), containsKey(), containsValue().
✔ Implemented by HashMap, TreeMap, LinkedHashMap, Hashtable.
✔ Does not extend Collection because it works differently (keys + values).

Map Hierarchy

plaintextCopy codeMap (Interface)
├── HashMap
├── TreeMap
├── LinkedHashMap
└── Hashtable

Example Using Map Interface

import java.util.*;

public class MapExample {
public static void main(String[] args) {
Map<Integer, String> studentMap = new HashMap<>(); // Map reference
studentMap.put(1, "Sujit");
studentMap.put(2, "Patel");

System.out.println(studentMap.get(1)); // Output: Sujit
}
}

🔹 Here, Map stores key-value pairs, allowing quick lookups using keys.


3. Key Differences Between Collection and Map

FeatureCollectionMap
TypeInterfaceInterface
FunctionStores a group of objectsStores key-value pairs
DuplicatesDepends on List (allows) or Set (disallows)Keys must be unique, but values can be duplicated
Order of ElementsList maintains order, Set does notHashMap is unordered, TreeMap is sorted
Key-Value StorageNo (only individual elements)Yes (key-value pairs)
ImplementationsArrayList, HashSet, LinkedListHashMap, TreeMap, LinkedHashMap

4. When to Use Collection vs Map?

  • Use Collection when storing a group of elements (List, Set, Queue).
  • Use Map when storing key-value pairs and needing quick lookups.

5. Conclusion

  • Collection is a root interface for data structures like List, Set, and Queue.
  • Map is a separate interface used for key-value storage.
  • Map does not extend Collection because it follows a different structure.

Would you like a comparison between HashMap and TreeMap? 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *