Collection Interface in Java – Free Java Tutorials

If we want to represent a group of individual objects as a single entity, then we should go for Collection Interface. Collection Interface defines the most common methods which are applicable for any Collection object like LinkedList, ArrayList etc.

Collection Interface Methods

These methods are declared inside Collection (Interface):

  • add(Object obj) :

This method we have to use when we have to add a single object to a Collection. Object ‘obj’ will be added in the collection in this case.

  • addAll(Collection c) :

This method is called when we have to add a group of objects to a Collection. In this case all objects inside Collection C will be added in the calling Collection.

  • remove(Object obj) :

This method is called when we have to remove a single object from a collection. Object ‘obj’ will be removed from the Collection in this case.

  • removeAll(Collection c) :

This method is called when we have to remove a group of objects from a Collection. In this case all objects inside Collection C will be removed from the calling Collection.

  • clear() :

This method is called when we have to clear all objects from a Collection. In this case not a single element will be there in the calling Collection.

  • retainAll(Collection c) :

This method is called when we have to remove every element except a group objects from the Collection. In this case, every element in the calling Collection will be removed except the elements in the Collection c.

  • contains(Object obj) :

This method is called to check whether an Object is present in the Collection or not. In this case the presence of the object ‘obj’ will be checked in the calling Collection.

  • containsAll(Collection c) :

This method is called to check whether a group of object is present inside a Collection or not. In this case the presence of every object inside Collection c will be checked inside the calling Collection.

  • isEmpty() :

This method is called to check whether a Collection is empty or not.

  • size() :

This method is call to check the size of a Collection.

  • toArray() :

This method is called to convert a Collection to an Array.

  • iterator() :

This method is called to get objects from a Collection one by one. Keep in mind iterator() method returns Iterator object, so we need an Iterator object to hold the value.

Now let’s take some programming related examples of these methods with their return types:

  • boolean add( Object obj )
  • boolean addAll( Collection c )
  • boolean remove( Object obj )
  • boolean removeAll( Collection c )
  • boolean retainAll( Collection c )
  • boolean contains( Object obj )
  • boolean containsAll( Collection c )
  • void clear()
  • int size()
  • Object[] toArray()
  • Iterator iterator()

Now, for every interface there must be an implementation class. But for Collection Interface there is no need of any concrete implementation class as there is no concrete implement class which implements Collection Interface directly.