Collection vs Arraylist: Which is Better?
In Java, Collection and ArrayList are related but different concepts. Collection is an interface, whereas ArrayList is a concrete implementation of the List
interface.
1. What is Collection?
Collection
is the root interface in the Java Collection Framework. It defines the most basic methods that all collection classes must implement.
Features of Collection Interface:
- Generic Interface – Can be implemented by various data structures (
List
,Set
,Queue
). - Basic Operations – Methods like
add()
,remove()
,contains()
, andsize()
. - Does Not Provide Direct Implementation – Instead, it is implemented by subinterfaces like
List
,Set
, andQueue
.
Hierarchy of Collection Interface:
plaintextCopy codeCollection (Interface)
├── List (Interface) → ArrayList, LinkedList, Vector
├── Set (Interface) → HashSet, TreeSet, LinkedHashSet
└── Queue (Interface) → PriorityQueue, ArrayDeque
Example Using Collection Interface
import java.util.*;
public class CollectionExample {
public static void main(String[] args) {
Collection<String> names = new ArrayList<>(); // Using Collection reference
names.add("Sujit");
names.add("Patel");
System.out.println(names);
}
}
🔹 Here, names
is declared as a Collection
, but it stores an ArrayList
.
2. What is ArrayList?
ArrayList
is a class that implements the List
interface. It is a resizable array implementation, meaning it can grow and shrink dynamically.
Features of ArrayList:
- Implements List Interface – Ordered collection, allows duplicates.
- Dynamic Resizing – Unlike arrays,
ArrayList
increases size automatically. - Fast Random Access – Uses an array internally, so retrieval by index is fast.
- Slow Insertion/Deletion – Shifting elements is required, making these operations slower.
- Allows Null Values – Unlike some collections like
TreeSet
,ArrayList
allowsnull
elements.
Example Using ArrayList
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
System.out.println(numbers.get(1)); // Output: 20
}
}
🔹 Here, ArrayList
is used directly and provides indexed access via get(index)
.
3. Key Differences Between Collection and ArrayList
Feature | Collection | ArrayList |
---|---|---|
Type | Interface | Class |
Implementation | Cannot be instantiated directly | Concrete class that implements List |
Methods | Defines general methods for collections | Provides specific implementations of List methods |
Flexibility | Can refer to any collection type (List , Set , Queue ) | Works only as a resizable array |
Usage | Used as a general reference | Used when a dynamic array is needed |
4. When to Use Collection vs ArrayList
- Use
Collection
when you want flexibility (e.g., you might switch betweenArrayList
,LinkedList
, orHashSet
). - Use
ArrayList
when you specifically need fast random access and a resizable array.
5. Conclusion
Collection
is an interface that defines general collection behavior.ArrayList
is a class that provides a concrete implementation ofList
.ArrayList
is often used with aCollection
reference for flexibility.
Would you like an example comparing ArrayList
with LinkedList
? 🚀