Collection vs Collections
TL;DR
Collectionis an interface (java.util.Collection). It is the root interface for the majority of the collections framework (extended byList,Set,Queue).Collectionsis a utility class (java.util.Collections). It contains only static methods that operate on or return collections (like sorting or searching).
Concept
It is very common for beginners to confuse the two because of the “s”.
Collection (Interface)
Defines the core methods that all collections must implement, such as:
add(E e)remove(Object o)size()iterator()clear()
Collections (Utility Class)
You cannot instantiate this class (its constructor is private). It acts purely as a helper class. It provides methods like:
Collections.sort(list)Collections.reverse(list)Collections.max(collection)Collections.synchronizedList(list)(Creates thread-safe wrappers)
Examples
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class CollectionVsCollections {
public static void main(String[] args) {
// 1. Collection (Interface)
// We use it as a reference type.
Collection<String> myCollection = new ArrayList<>();
myCollection.add("Java");
myCollection.add("Python");
myCollection.add("C++");
// 2. Collections (Utility Class)
// We call static methods on it to manipulate the Collection.
// Note: sort requires a List, not just a Collection!
List<String> myList = (List<String>) myCollection;
Collections.sort(myList);
System.out.println("Max element: " + Collections.max(myCollection));
// Creates an unmodifiable wrapper
Collection<String> readOnly = Collections.unmodifiableCollection(myCollection);
// readOnly.add("Go"); // Throws UnsupportedOperationException
}
}
Interview Questions
Q: Can you create an object of the Collections class?
A: No. The java.util.Collections class has a private constructor to prevent instantiation. It is designed solely as a holder for static utility methods, similar to the java.lang.Math class.
Q: Does the Map interface extend the Collection interface?
A: No. Map operates on key-value pairs, while Collection operates on single elements. Because their fundamental nature is different (e.g., add(element) makes no sense for a map, which requires put(key, value)), Map does not extend Collection. However, you can obtain a Collection view of a Map using map.values() or map.keySet().