Java Collections Framework

Java Collections Tutorial List :

Introduction to Java Collections :

In Java we can store data using Arrays. Size of Array is fixed which is defined during initialization of the array.I real time, developer require a data structure which is flexible in size, so that data can be added and removed from this data structure on request. To avoid that every developer has to implement his custom data structure which requires more coding effort. The Java library provide several default implementations for this via the collection framework.

A collection represents a group of objects, known as its elements. Collections in java is a framework that provides an architecture to store and manipulate the group of objects.

All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections Framework.

Collections can be used in various scenarios like Storing phone numbers, Employee names etc.

Hierarchy of Collection Framework :


The collection framework has a lot of Interfaces. Some important Interfaces in the collection framework is discussed below.

Interfaces of Collection Framework :

It is at the top of collection hierarchy and must be implemented by any class that defines a collection. Its general syntax is given below.

interface Collection < E >

Methods of Collection interface :

Method Description
public boolean add(Object element) used to insert an element in this collection.
public boolean addAll(collection c)used to insert the specified collection elements in the invoking collection.
public boolean remove(Object element)used to delete an element from this collection.
public boolean removeAll(Collection c)used to delete all the elements of specified collection from the invoking collection.
public boolean retainAll(Collection c)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)used to search an element.
public boolean containsAll(Collection c)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.

Iterator interface :

Iterator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are mentioned below.

  1. public boolean hasNext() it returns true if iterator has more elements.
  2. public object next() it returns the element and moves the cursor pointer to the next element.
  3. public void remove() it removes the last elements returned by the iterator. It is rarely used.

List Interface :

It extends the Collection Interface, and it will store element as sequence. It will allow random access and insertion, based on position.It allows Duplicate elements. Following is its general declaration.
interface List < E >

A List is an ordered Collection and it can contain duplicate elements. Elements can be inserted or accessed by their position in the list, using a zero-based index.

The three implementations of the List interface are Vector, ArrayList and Linked List.

Set Interface :

This interface defines a Set. It extends Collection interface and doesn't allow insertion of duplicate elements. It's general declaration is,

interface List < E >

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.

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

The three main implementations of the Set interface are HashSet, TreeSet, and LinkedHashSet.

Queue Interface :

This interface defines a Set. It extends Collection interface and doesn't allow insertion of duplicate elements. It's general declaration is,

interface Queue < E >

DeQueue Interface :

It extends Queue interface and declares behaviour of a double-ended queue. Its general declaration is,

interface DeQueue < E >

comments powered by Disqus