Collection Interface in Java

 

Introduction:

In this article, we are going to discuss Collection Interface in Java their types and methods. We can also discuss all methods with programming examples.

This topic is very important because in every interview some questions are coming related to this topic. so we need to understand all the things on this topic.

 

Collection Interface:

When we are representing a group of individual objects in a single entity then we should go for Collection Interface.

The Collection interface is the root interface of the collection framework. The collection framework holds all the collection classes and interfaces.

The collection interface defines the most common methods which are applicable for any collection object.

The Collection interface present in (java.util.Collection) package and Map interface present in (java.util.Map) package, are the two main “root” interfaces of Java collection classes.

 

Collection Interface in java

In the above diagram, we can see the structure of the collection interface. It is introduced in java 1.2 version. Collection interface has multiple child interfaces.

  • List Interface
  • Set Interface
  • Queue Interface

 

What is Collections:

The collections is a utility class that is present in java.util package, it defines several utility methods for collection objects.

Note: Always remember that Collection is an interface and Collections is a Class.

 

Methods of the Collection Interface:

Collection Interface defines the Most Common Methods which are Applicable for any Collection Objects.

List of the Methods Presents Inside Collection Interface.

  • boolean add(Object o) -> It is used to add an object to this collection.                                                                       
  • boolean addAll(Collection c) -> Using this method adds all the elements in the given collection to this collection. If in this collection any duplicate values are present, then this method removes the first occurrence of the object.                                                                                                                                                                                                          
  • boolean removeAll(Collection c) -> Using this method we can remove all the objects mentioned in the given collection from the collection                                                                                                                                                                                                                         
  • boolean retainAll(Collection c) -> Using this method we can retain only the elements in this collection that are contained in the specified collection.                                                                                                                               
  • void clear() -> Using this method we can remove all of the elements from this collection.                                               
  • boolean contains(Object o) -> Using this method we can returns true if the collection contains the specified element.                                                                                                                                                                                                
  • boolean containsAll(Collection c) -> Using this method we can returns true if the collection contains all of the elements in the given collection.                                                                                                                                                  
  • boolean isEmpty() -> Using this method we can returns true if this collection contains no elements.                         
  • int size() -> Using this method we can return the size of elements in the collection.                                                                     
  • int max() -> Using this method we can return the maximum value present in the collection.                                            
  • Iterator iterator() -> Using this method we can iterator over the elements in this collection.                                                                      
  • Object[] toArray() -> Using this method we can converts collection into array.                                                                   
  • int hashCode() -> Using this method we can return the hash code value for this collection.                                          
  • equals(Object o) -> Using this method we can compare the equality of two objects.                                                                                                   

Note:

  • There is No Concrete Class that implements Collection Interface Directly.
  • There is No Direct Method in Collection Interface to get Objects.

 

Read More topics related to the collection.

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

Leave a Reply

Your email address will not be published.