DEV Community

Cover image for Collection vs Collections in Java: What's the Difference?
Vidya
Vidya

Posted on

Collection vs Collections in Java: What's the Difference?

When I first started learning Java, I often got confused between Collection and Collections. Their names are almost identical, but they serve completely different purposes.

Let's understand the difference with simple explanations and examples.

What is Collection?

Collection is an interface in the Java Collection Framework. It represents a group of objects (elements) and defines the basic operations that all collection classes should support.

Common methods include:
--> add()
--> remove()
--> contains()
--> size()
--> isEmpty()

Some classes that implement the Collection interface are:
=> ArrayList
=> LinkedList
=> HashSet
=> PriorityQueue

Example

import java.util.*;

public class CollectionExample {
    public static void main(String[] args) {

        Collection<String> fruits = new ArrayList<>();

        fruits.add("Apple");
        fruits.add("Mango");
        fruits.add("Orange");

        System.out.println(fruits);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

[Apple, Mango, Orange]
Enter fullscreen mode Exit fullscreen mode

Here, Collection acts as the interface, while ArrayList provides the implementation.

What is Collections?
Collections is a utility class in Java.
It contains static methods that help perform operations on collection objects.

Some commonly used methods are:
--> sort()
--> reverse()
--> shuffle()
--> max()
--> min()
--> frequency()

Example

import java.util.*;

public class CollectionsExample {
    public static void main(String[] args) {

        List<Integer> numbers = new ArrayList<>();

        numbers.add(30);
        numbers.add(10);
        numbers.add(20);

        Collections.sort(numbers);

        System.out.println(numbers);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

[10, 20, 30]
Enter fullscreen mode Exit fullscreen mode

Here, Collections.sort() sorts the elements in ascending order.

Easy Way to Remember:
Collection → Stores Data
Collections → Performs Operations on Data

Think of it like this:
Collection = A box that holds your items.
Collections = A toolbox with utilities to organize and manipulate those items.

Top comments (0)