Collection vs Collections in java

Both Collection and Collections in Java, have some similarities like

  • Both (Collection and Collections) are part of the Java Collections Framework.
  • Both (Collection and Collections) are present in java.util package.
  • Both (Collection and Collections) are added to jdk in java version 1.2

Besides these similarities, they have few differences also.

Collection

Collection interface represents the root interface in the collection hierarchy. It contains the core methods for all collections. List, Set and Queue are immediate sub interfaces of Collection interface.

Map interface is also part of Java collection framework but it does not inherit the Collection interface.

Syntax:

public interface Collection extends Iterable

Collection Interface Methods

add() It is used to add the new element in the specified collection.
remove(Object o) It is used to remove the existing element from the specified collection.
size() It is used to get the number of elements in the specified collection.
clear() It is used to remove the all elements from the specified collection.
isEmpty() It is used to check whether the specified collection contains any element or not. It returns true if one or more element present the collection otherwise returns false.

Collections

Collections class in java represents an utility class in java.util package. It contains exclusively static methods that operate on or return collections. It consists of polymorphic algorithms that operate on collections, “wrappers”, which in turn return a new collection backed by a specified collection.

Syntax:

public class Collections extends Object

Collections Class Methods

Collections.sort() It is used to sort the elements in the specified collection.
Collections.min() It is used to get the minimum element in the specified collection.
Collections.max() It is used to get the maximum element in the specified collection.
Collections.binarySearch() It is used to search the specified element in the given collection. It uses binary search algorithm internally.
Collections.reverse() It is used to reverse the order of elements in the specified collection.
Collections.copy() It is used to copy all elements from one collection to the another collection.
Collections.shuffle() It is used to randomly shuffles the elements in the specified collection.
Collections.synchronizedCollection() It is used to synchronize the specified collection.
Collections.disjoint() It returns true if two given collections have no elements in common, otherwise returns false.

 
Example

import java.util.*; 
public class Main {  
 
    public static void main(String args[]) 
    { 
        //Createing ArrayList Object
        ArrayList<String> students = new ArrayList<>(); 
 
        //Adding elements to the ArrayList
        students.add("Jai");
        students.add("Manish");
        students.add("Vikram");
        students.add("Mahesh");
        students.add("Naren");
 
        //Print ArrayList
        System.out.println("ArrayList elements: " + students);
 
        // Print minimum value
        System.out.println("Minimum value: " + Collections.min(students));
 
        // print maximum value
        System.out.println("Maximum value: " + Collections.max(students));
    }  
}

Output

ArrayList elements: [Jai, Manish, Vikram, Mahesh, Naren]
Minimum value: Jai 
Maximum value: Vikram

Difference between Collection and Collections

Collection Collections
It represents root level interface of Java Collection framework. It represents as utility class of Java Collection framework which consists of only static methods that operate on or return collections.
Since Java 8, It can contains static, abstract and default methods. It can only contains static methods.
It extends iterable interface. It extends Object class.

Java interview questions on collections