
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences Between Collection and Collections in Java
In Java, Collection and Collections are important components of the Collections Framework. The Collection has various sub-interfaces such as Set, List, and Queue.
The Collections provides static methods to perform various operations on collections, such as sorting, searching, and synchronization.
Let's learn them one by one with proper definitions, syntax, and a suitable example.
The Collection Interface
In Java, the Collection is an interface, which is considered the root interface in the collection hierarchy. It represents a group of objects, which are known as its elements.
Some collections allow duplicate values and specific orders, whereas some do not allow duplicates and do not maintain any order. Here are a few important methods of this interface:
- add()
- remove()
- size()
- clear()
Syntax
Following is the syntax of the Collection interface:
Interface Collection<E>
Where the E is the type of elements in this collection.
Example
In the following example, we create a collection named languages using the ArrayList class and perform various operations such as size(), remove(), etc, to get the size and remove collection elements:
import java.util.*; public class CollectionTest { public static void main(String[] args) { // Creating a Collection using ArrayList Collection<String> langauages = new ArrayList<>(); // adding elements to it langauages.add("Java"); langauages.add("Python"); langauages.add("C++"); // Displaying elements System.out.println("The collection elements are: " + langauages); //displaying size System.out.println("The collection size is: " + langauages.size()); // Removing an element langauages.remove("Python"); System.out.println("Collection after removing element: "); // Iterating through elements for (String lang : langauages) { System.out.println(lang); } } }
The above program produces the following output:
The collection elements are: [Java, Python, C++] The collection size is: 3 Collection after removing element: Java C++
The Collections Class
In Java, the Collections is a utility class of the Object class. The Collections class provides various static methods (i.e., call directly using the class name) that operate on or return collections.
Here are a few important static methods of this utility class:
- sort()
- min()
- max()
- fill()
- copy()
- reverse()
Syntax
Following is the syntax of the Collections utility class in Java:
public class Collections extends Object
Example
In the following example, we use the Collections class's static methods add(), sort(), min(), and max() to add, sort, and find min and max values in an ArrayList class:
import java.util.*; public class CollectionTest { public static void main(String args[]) { //creating ArrayList ArrayList<Integer> list = new ArrayList<>(); // Adding elements to the ArrayList list.add(5); list.add(35); list.add(20); list.add(65); list.add(50); //iterating over ArrayList System.out.println("ArrayList elements are: "); for(Integer i : list){ System.out.print(i + " "); } //sort the ArrayList Collections.sort(list); //arraylist after sorting System.out.println("\nArrayList after sorting: "); for(Integer i : list){ System.out.print(i + " "); } //retrieve minimum value using Collections class System.out.println("\nMinimum value: " + Collections.min(list)); //retrieve maximum value using collections class System.out.println("Maximum value: " + Collections.max(list)); } }
Below is the output of the above program:
ArrayList elements are: 5 35 20 65 50 ArrayList after sorting: 5 20 35 50 65 Minimum value: 5 Maximum value: 65
Difference between Collection and Collections in Java
Below is a list of a few differences between Collection and Collections:
Feature | Collection | Collections |
---|---|---|
Type | It is an Interface. | It is a Utility Class. |
Methods | It supports Abstract, Default, and static methods (since Java 8). | It only has static methods. |
Usage | It is used to represent a group of individual objects as a single unit. | It provides various utility methods that are used to operate on a collection. |