Collection vs Collections in Java with Example

Collection: Collection is a interface present in java.util.package. It is used to represent a group of individual objects as a single unit. It is similar to the container in the C++ language. The collection is considered as the root interface of the collection framework. It provides several classes and interfaces to represent a group of individual objects as a single unit. 

The List, Set, and Queue are the main sub-interfaces of the collection interface. The map interface is also part of the java collection framework, but it doesn’t inherit the collection of the interface. The add(), remove(), clear(), size(), and contains() are the important methods of the Collection interface.

Declaration:

public interface Collection<E> extends Iterable<E>

Type Parameters: E - the type of elements returned by this iterator

Collections: Collections is a utility class present in java.util.package. It defines several utility methods like sorting and searching which is used to operate on collection. It has all static methods. These methods provide much-needed convenience to developers, allowing them to effectively work with Collection Framework. For example, It has a method sort() to sort the collection elements according to default sorting order, and it has a method min(), and max() to find the minimum and maximum value respectively in the collection elements.

Declaration:



public class Collections extends Object

Collection vs Collections:                                                           

                                  Collection                                           Collections
It is an interface. It is a utility class.
It is used to represent a group of individual objects as a single unit.  It defines several utility methods that are used to operate on collection.
The Collection is an interface that contains a static method since java8. The Interface can also contain abstract and default methods. It contains only static methods.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the difference  
// between Collection and Collections
  
import java.io.*;
import java.util.*;
  
class GFG {
    
    public static void main (String[] args) 
    {
        
      // Creating an object of List<String>
      List<String> arrlist = new ArrayList<String>(); 
        
      // Adding elements to arrlist
      arrlist.add("geeks");
      arrlist.add("for");
      arrlist.add("geeks");
        
      // Printing the elements of arrlist
      // before operations
      System.out.println("Elements of arrlist before the operations:");
      System.out.println(arrlist);
        
      System.out.println("Elements of arrlist after the operations:");
        
      // Adding all the specified elements
      // to the specified collection
      Collections.addAll(arrlist, "web", "site");
        
      // Printing the arrlist after
      // performing addAll() method
      System.out.println(arrlist);
        
      // Sorting all the elements of the  
      // specified collection according to 
      // default sorting order
      Collections.sort(arrlist);
          
      // Printing the arrlist after
      // performing sort() method
      System.out.println(arrlist);
          
    }
}

chevron_right


Output

Elements of arrlist before the operations:
[geeks, for, geeks]
Elements of arrlist after the operations:
[geeks, for, geeks, web, site]
[for, geeks, geeks, site, web]

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.