The Java Collection Framework is a vast and very important feature of Java. Though there
are many resources to learn Java, I felt there was something missing. Initially, we may be
confused which Collection to use when. This is the purpose of this tutorial. This tutorial will
help you decide which Collection satisfies your needs by comparing all of them.
People experienced in Java feel free to check the tutorial and comment if there are any
The Java Collection Framework(JCF) standardizes the way in which groups of objects
are handled. The Collections can store pre-defined objects present in Java like Integer,
Double, String etc and user defined objects. There are several interfaces and classes in
the framework which I will discuss in the tutorial. As it is a huge topic, I would like to
separate the tutorial into 4 parts.
This part contains a brief introduction to the Collection Framework. The important concepts
to be discussed in the Framework are -
1) Collection Interfaces and Classes - These define several core interfaces. The
collection interfaces determine the basic functionality of Collection classes.
2) Map Interfaces and Classes - These interfaces and classes are used to store elements
in the form of (key/value) pairs.
3) Legacy Interfaces and Classes - Before the addition of Collection Framework, few
classes were used to store group of objects. These classes are called Legacy Classes.
4) Iterators - Iterators enable you to cycle through a collection, obtaining or removing
elements. The types of iterators will be discussed later in this tutorial.
5) Algorithms - The collection framework defines several algorithms that can be applied to
collections and maps. They always provide efficient implementation of the algorithm.
6) Comparators - It is the comparator which defines the sorted order of collections. It can
be used with only those collections which store the elements in sorted order.
7) Random Access Interface - The collections implementing this interface signals that it
supports efficient random access to its elements.
Note for C++ users: Java does not have the concept of pair<>. Therefore, when you want
to store pairs of objects, you have to use one of the Map classes. An alternative is to create
a class which stores pairs of objects and a Collection to store these objects.
Let’s get into detail by first looking at iterators.
Iterators are used to cycle through or access all the elements of a collection in a particular
STEPS TO USE AN ITERATOR:
Obtain an iterator to the start of the collection by calling collection’s iterator() method.
Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext()
Within the loop, obtain each element by calling next().
TYPES OF ITERATOR:
Java Collections Tutorial Series: