Friday, December 27, 2024
Google search engine
HomeLanguagesJavaCollection vs Collections in Java with Example

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




// 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);
          
    }
}


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]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments