Collections in Java and Collection Framework

Collections in java is a framework. It is are one of the most commonly reusable data structures. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet etc.

Java collections are one of the most commonly used data-structures by all Java professionals. But are you using the right collection class that would best suits your need. Most programmers usually use Vectors, ArrayList, HashMap or the Hashtable. There are many other collection classes available with the JDK that you can use instead of re-inventing logic to suite your needs.

We will be trying to understand the different types of classes and when each Collection class could be used. We wouldn’t be looking into the implementation details of any collection, for that please refer the latest Java Collection API docs.

The Core Collection Framework Interfaces
The core collection frameworks are depicted in the following image.

Collections in Java

The main type of collections are:

  • Sets
  • Lists
  • Queues
  • Maps

Maps are not an integral part of the Collection framework, but they are still considered as Collection because of their capability to store and manipulate data as collection of objects.

Sorted Sets and Sorted Maps are basically a sorted version of Sets and Maps.
Hierarchy of Collection Framework
Let us see the hierarchy of collection framework.The java.util package contains all the classes and interfaces for Collection framework.

Collection Framework in Java

Goals for Java Collections:
The collections framework was designed to meet several goals.

  • The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.
  • The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.
  • Extending and/or adapting a collection had to be easy.

Commonly used methods of Collection interface:
There are many methods declared in the Collection interface. They are as follows:

  1. public boolean add(object element): is used to insert an element in this collection.
  2. public boolean addAll(collection c): is used to insert the specified collection elements in the invoking collection.
  3. public boolean remove(object element): is used to delete an element from this collection.
  4. public boolean removeAll(Collection c): is used to delete all the elements of specified collection from the invoking collection.
  5. public boolean retainAll(Collection c): is used to delete all the elements of invoking collection except the specified collection.
  6. public int size(): return the total number of elements in the collection.
  7. public void clear(): removes the total no of element from the collection.
  8. public boolean contains(object element): is used to search an element.
  9. public boolean containsAll(collection c): is used to search the specified collection in this collection.
  10. public Iterator iterator(): returns an iterator.

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:

  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.

What we will learn in Collection Framework ?


<<Previous <<   || Index ||   >>Next >>