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.
The main type of collections are:
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.
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.
There are many methods declared in the Collection interface. They are as follows:
- 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.
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:
- public boolean hasNext() it returns true if iterator has more elements.
- public object next() it returns the element and moves the cursor pointer to the next element.
- public void remove() it removes the last elements returned by the iterator. It is rarely used.
What we will learn in Collection Framework ?