Java Interface Collection

Java Interface Collection

Summary: Java Interface Collection is the super most interface from which almost all DS are derived. Explained the API in simple terms for a very beginner.

Collection interface forms the base from which many data structures (almost all) are derived. The data structures derived from Collection interface are known as collections framework. Two main sub interfaces are List and Set. Collection interface methods are overridden by different classes in different ways as per their requirement. Some collection classes do not accept duplicate values like HashSet and TreeSet. Some DS throw NullPointerException when an ineligible element (like null) added and some throw ClassCastException. Some DS do not add and simply returns false. The Collection interface comes with many methods so that each class derived can use well. Following is the interface signature.

public interface Collection extends Iterable

This interface is implemented by a wide variety of classes with different restrictions on the elements they store. Some implemented classes do not allow null elements and some do not allow duplicate elements. Some common unchecked exceptions thrown by these classes are NullPointerException (if the collection is added null elements when the data structure does not support) and ClassCastException (if the collection contains incompatible elements).

Following are a few methods of Java Collection interface overridden with body by many subclasses.

  • boolean add(Object obj): Adds an element obj to the DS. If the element is added successfully, this method returns true.
  • boolean remove(Object obj): Removes the object obj from the DS. If available and removed successfully, this method returns true.
  • boolean containsAll(Collection col): Useful to compare the elements of two different DS. Returns true if both the DS contains the same elements. With one collection class object call the method and the other, say col, pass as parameter.
  • boolean addAll(Collection col): Useful to add elements of one DS to the other col, passed as parameter; it is the easiest way (avoids something like a for loop or copy method etc.) If added successfully, returns true else false.
  • boolean removeAll(Collection col): Useful to remove the elements in one DS that also exist in another. That is, the elements of the DS that called the method are removed that also exist in the DS col passed as parameter. Returns true if the removal is successful.
  • boolean retainAll(Collection col): Just opposite to removeAll(). This method keeps the elements that also exist in the DS, col passed as parameter. Other way to say, removes the elements in the current DS (that called the method) that do not exist in col.
  • void clear(): Removes all the elements of the DS. After this method call, the size of the DS is zero.
  • boolean equals(Object obj): Used to compare two DS objects for equality.
  • boolean isEmpty(): Checks the DS and returns true if the DS does not contain any elements. False return value indicates elements exist in the DS.
  • Iterator iterator(): Returns an Iterator object useful to iterate the elements of the DS.
  • Object[] toArray(): Returns the elements of the DS as an Object array. Useful to print the values in a for loop using array. With arrays, retrieval is faster.
  • int hashCode(): Returns the hashcode value of the collection object.

The above methods are used in many subsequent programs for illustration. The DS that does not support a few of the above methods, when called with an object, throws UnsupportedOperationException.

3 thoughts on “Java Interface Collection”

  1. ADITHYA VARDHAN DECOLU

    The rule is that… “when we implement an interface, we need to override all the methods of that interface”
    here we are implementing Collection interface. then why don’t we override all the 14 methods?
    (sir i am new to java, please help me out with this ) :(

Leave a Comment

Your email address will not be published.