Collection vs Collections: What is Difference?
In Java, Collection and Collections are different concepts despite their similar names.
Collection
is an interface that represents a group of objects.Collections
is a utility class that provides static methods for working with collections.
1. What is Collection?
Collection
is the root interface of the Java Collection Framework. It defines common methods that all collection classes (like List
, Set
, Queue
) must implement.
Features of Collection Interface:
✔ Defines methods like add()
, remove()
, size()
, contains()
.
✔ Extended by List
, Set
, and Queue
.
✔ Cannot be instantiated directly (it needs an implementation like ArrayList
or HashSet
).
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); // Output: [Sujit, Patel]
}
}
🔹 Here, Collection
is used as a reference for ArrayList
.
2. What is Collections?
Collections
is a utility class in java.util
that provides static methods for working with collections.
Features of Collections Class:
✔ Provides utility methods like sorting, searching, shuffling, and synchronization.
✔ Works with implementations of Collection
(List
, Set
, Queue
).
✔ Cannot be instantiated since all methods are static
.
Common Methods in Collections Class
Collections.sort(List<T>)
– Sorts a list in ascending order.Collections.reverse(List<T>)
– Reverses the order of elements in a list.Collections.shuffle(List<T>)
– Shuffles elements randomly.Collections.max(Collection<T>)
– Finds the maximum element.Collections.min(Collection<T>)
– Finds the minimum element.Collections.synchronizedList(List<T>)
– Returns a thread-safe list.
Example Using Collections Class
import java.util.*;
public class CollectionsExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(30, 10, 50, 20));
Collections.sort(numbers); // Sorting the list
System.out.println("Sorted List: " + numbers); // Output: [10, 20, 30, 50]
Collections.reverse(numbers); // Reversing order
System.out.println("Reversed List: " + numbers); // Output: [50, 30, 20, 10]
}
}
🔹 Here, Collections.sort()
and Collections.reverse()
manipulate the ArrayList
.
3. Key Differences Between Collection and Collections
Feature | Collection | Collections |
---|---|---|
Type | Interface | Utility class |
Function | Represents a group of objects | Provides utility methods for collections |
Methods | Defines add() , remove() , size() , etc. | Provides sort() , reverse() , shuffle() , etc. |
Instantiation | Cannot be instantiated | Cannot be instantiated (only has static methods) |
Usage | Used as a base for data structures | Used to manipulate and manage collections |
4. When to Use Collection vs Collections?
- Use
Collection
when defining a group of objects (List
,Set
,Queue
). - Use
Collections
when you need utility functions like sorting, searching, or synchronization.
5. Conclusion
Collection
is an interface for data structures (List
,Set
,Queue
).Collections
is a utility class that provides helper methods.
Would you like more examples on sorting or synchronization? 🚀