Interfaces of Collection Framework

The collection framework has a lot of Interfaces, setting the fundamental nature of various collection classes. Lets study the most important Interfaces in the collection framework.


The Collection Interface

  1. It is at the top of collection heirarchy and must be implemented by any class that defines a collection. Its general declaration is,
    interface Collection < E >
    
  2. Following are some of the commonly used methods in this interface.
    MethodsDescription
    add( E obj )Used to add objects to a collection. Doesn't add duplicate elements to the collection.
    addAll( Collection C )Add all elements of collection C to the invoking collection
    remove( Object obj )To remove an object from collection
    removeAll( Collection C )Removes all element of collection C from the invoking collection
    contains( Object obj )To determine whether an object is present in collection or not
    isEmpty()Returns true if collection is empty, else returns false
    size()returns number of elements present in collection

The List Interface

  1. It extends the Collection Interface, and defines storage as sequence of elements. Following is its general declaration,
    interface List < E >
    
  2. Allows random access and insertion, based on position.
  3. It allows Duplicate elements.
  4. Apart from methods of Collection Interface, it adds following methods of its own.
    MethodsDescription
    get( int index )Returns object stored at the specified index
    set( int index, E obj)Stores object at the specified index in the calling collection
    indexOf( Object obj )Returns index of first occurence of obj in the collection
    lastIndexOf( Object obj )Returns index of last occurence of obj in the collection
    subList( int start, int end )Returns a list containing elements between start and end index in the collection

The Set Interface

  1. This interface defines a Set. It extends Collection interface and doesn't allow insertion of duplicate elements. It's general declaration is,
    interface Set < E >
    
  2. It doesn't define any method of its own. It has two sub interfaces, SortedSet and NavigableSet.
  3. SortedSet interface extends Set interface and arranges added elements in an ascending order.
  4. NavigabeSet interface extends SortedSet interface, and allows retrieval of elements based on the closest match to a given value or values.

The Queue Interface

  1. It extends collection interface and defines behaviour of queue, that is first-in, first-out. It's general declaration is,
    interface Queue < E >
    
  2. There are couple of new and interestin methods added by this interface. Some of them are mentioned in below table.
    MethodsDescription
    poll()removes element at the head of the queue and returns null if queue is empty
    remove()removes element at the head of the queue and throws NoSuchElementException if queue is empty
    peek()returns the element at the head of the queue without removing it. Returns null if queue is empty
    element()same as peek(), but throws NoSuchElementException if queue is empty
    offer( E obj )Adds object to queue.

The Dequeue Interface

  1. It extends Queue interface and declares behaviour of a double-ended queue. Its general declaration is,
    interface Dequeue < E >
    
  2. Double ended queues can function as simple queues as well as like standard Stacks.