Interfaces of Collection Framework in java

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


Collection Interface

It is at the top of collection heirarchy and must be implemented by any class that defines a collection. It represents a group of objects as a single object. There are many methods declared in the Collection interface. They are as follows:

Method Description
public boolean add(Object element) is used to insert an element in this collection.
public boolean addAll(Collection c) is used to insert the specified collection elements in the invoking collection.
public boolean remove(Object element) is used to delete an element from this collection.
public boolean removeAll(Collection c) is used to delete all the elements of specified collection from the invoking collection.
public boolean retainAll(Collection c) is used to delete all the elements of invoking collection except the specified collection.
public int size() return the total number of elements in the collection.
public void clear() removes the total no of element from the collection.
public boolean contains(Object element) is used to search an element.
public boolean containsAll(Collection c) is used to search the specified collection in this collection.
public Iterator iterator() returns an iterator.
public Object[] toArray() converts collection into array.
public boolean isEmpty() checks if collection is empty.
public boolean equals(Object element) matches two collection.
public int hashCode() returns the hashcode number for collection.

List Interface

It is the sub-interface of Collection interface which provides the facility of performing operation with indexed collection of elements which may or not contain duplicates element in it.
Methods of Java List Interface

Method Description
void add(int index,Object element) It is used to insert element into the invoking list at the index passed in the index.
boolean addAll(int index,Collection c) It is used to insert all elements of c into the invoking list at the index passed in the index.
object get(int index) It is used to return the object stored at the specified index within the invoking collection.
object set(int index,Object element) It is used to assign element to the location specified by index within the invoking list.
object remove(int index) It is used to remove the element at position index from the invoking list and return the deleted element.
ListIterator listIterator() It is used to return an iterator to the start of the invoking list.
ListIterator listIterator(int index) It is used to return an iterator to the invoking list that begins at the specified index.

Set Interface

This interface defines a Set. It extends Collection interface and doesn't allow insertion of duplicate elements

  It doesn't define any method of its own. It has two sub interfaces, SortedSet and NavigableSet.

  SortedSet interface extends Set interface and arranges added elements in an ascending order or based on Comparator..

  NavigabeSet interface extends SortedSet interface, and allows retrieval of elements based on the closest match to a given value or values.

It's general declaration is,

interface Set < E >

Queue Interface

It extends collection interface and defines behaviour of queue, that is first-in, first-out.

Method Description
public Object poll() removes element at the head of the queue and returns null if queue is empty.
public Object remove() removes element at the head of the queue and throws NoSuchElementException if queue is empty
public Object peek() returns the element at the head of the queue without removing it. Returns null if queue is empty
public Object element() same as peek(), but throws NoSuchElementException if queue is empty
public boolean offer( E obj ) Adds object to queue.

Dequeue Interface

It extends Queue interface and implements behaviour of a double-ended queue.

  Since it implements Queue interface, it has the same methods as mentioned there.

  Double ended queues can function as simple queues as well as like standard Stacks.

woman avatar