• March 26, 2025

Collection vs List Java: What is Difference?

Collection vs List in Java

In Java, Collection and List are both part of the Java Collection Framework (JCF), but they have different roles:

  • Collection is a root interface that represents a group of objects.
  • List is a subinterface of Collection that maintains an ordered collection of elements.

1. What is Collection?

Collection is the top-level interface in the Java Collection Framework that defines basic methods for managing a group of objects.

Features of Collection Interface:

Root interface for all collection classes (List, Set, Queue).
✔ Defines fundamental methods like add(), remove(), size(), and contains().
✔ Does not allow direct object instantiation (needs implementation classes like ArrayList, 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<Integer> numbers = new ArrayList<>(); // Collection reference
numbers.add(10);
numbers.add(20);

System.out.println(numbers); // Output: [10, 20]
}
}

🔹 Here, Collection is used as a reference type, but ArrayList is the actual implementation.


2. What is List?

List is a subinterface of Collection that represents an ordered collection (sequence) of elements.

Features of List Interface:

Maintains insertion order (unlike Set).
Allows duplicate elements.
✔ Supports index-based access (get(index)).
✔ Implemented by ArrayList, LinkedList, Vector.

Example Using List Interface

import java.util.*;

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

System.out.println(names.get(1)); // Output: Patel
}
}

🔹 List allows duplicate values and supports index-based access.


3. Key Differences Between Collection and List

FeatureCollectionList
TypeInterfaceSubinterface of Collection
FunctionGeneral framework for collectionsOrdered collection of elements
Order of ElementsNo specific orderMaintains insertion order
DuplicatesVaries (Set disallows, List allows)Allows duplicates
Index AccessNo direct index accessSupports get(index) and set(index, value)
Implementation ClassesArrayList, HashSet, TreeSet, LinkedList, etc.ArrayList, LinkedList, Vector

4. When to Use Collection vs List?

  • Use Collection when you need a flexible reference type for different data structures (List, Set, Queue).
  • Use List when you need ordered storage and index-based access.

5. Conclusion

  • Collection is a generic interface that covers List, Set, and Queue.
  • List is a specific subinterface of Collection that maintains order and allows duplicates.

Would you like a comparison between ArrayList and LinkedList? 🚀

Leave a Reply

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