Collection vs Collections in Java

Collections, with an “s”, in Java is a utility class. The Collections class provides generic algorithms that are used for sorting, shuffling, searching, copying, filling, and reversing. Collection, without an “s”, is an interface which is also the root interface in the hierarchy from which most of the interfaces in the Java Collection Framework derive. For example, the List, Queue, and Set interfaces all derive from the Collection interface. But, note that the Map interface does NOT derive from the Collection interface.

Aside from being the name for a root interface, the term collection is used to refer to an object that stores data. You can also think of a collection as a container. Most collections support the fundamental operations of insert, retrieve, and iterate.

How to get a concrete Collection class?

You might be wondering what’s the point of just having an interface hierarchy – what about the actual concrete classes which implement the interface(s)? Of course, there are concrete classes provided by Java that implement those interfaces. They are in the java.util package. One example would be the popular ArrayList class, which implements the List interface. Another popular class is HashMap, which implements the Map interface – HashMap is commonly used as a hashtable implementation in Java.

What does the Collections class do?

Earlier we mentioned that the Collections class is a utility class. Note that the meaning of a utility class is a class that defines a set of methods that perform common, often re-used functions. The Collections class contains a lot of static methods which are used to implement the algorithms mentioned earlier – like sort(), shuffle(), etc.

Because the methods are static you will see a lot of calls that use the Collections class directly, which will look like something that starts with “Collections.”, for example – Collections.sort(someArgument).

Those methods are used to perform operations on collections – so actual Collection types (which are of type Collection, where Collection is the interface). An example of when we would want to use a Collections method is if we have an ArrayList, and we want to sort all the elements in that ArrayList. We could simply make a call like this: Collections.sort(someArrayList).

So, we could summarize the difference between the Collections class and Collection interface by saying that Collections and Collection, although different things, are very much related. This is because Collections is a utility class with methods that are meant to operate on concrete classes that are of the Collection type.

Note that some of the Collections class methods can also be used on classes that implement the Map interface – which do not derive from the Collection interface and for that reason are not of the Collection type.

This is a common interview question that you should definitely know when preparing for a Java interview.





Follow Varoon Sahgal, author, on Google Plus