Getting started with Java Collection Framework

Avinash Tingre
Javarevisited
Published in
6 min readDec 21, 2020

--

In this article, we will try to understand Java Collection Framework from very basic. This will be a series of articles which will cover every aspect of Collection in step-by-step manner. Let’s get started.

Need for Collection Framework 💡

Let’s suppose, you want to represent huge number of values then using separate variable for each value is not a good programming practice. Hence, to overcome this problem, we can go for Array concept.

Array
An Array is an indexed collection of fixed number of homogeneous data elements. The main advantage of arrays is, we can represent multiple values by using single variable. So that readability of code will be improved.

Limitations of Arrays ⚠️

  1. Arrays are fixed in size i.e. Once we create an array, there is no chance of increasing or decreasing the size based on our requirement. Due to this, to use the arrays concept, compulsory we should know the size in advance, which may not be possible always.
  2. Array can hold only homogeneous data type elements.

However, we can solve this problem by using Object type arrays.

3. Arrays concept is not implemented based on some standard data structure and hence readymade method support is not available. For every requirement, we have to write the code explicitly. Which increases complexity of programming.

To overcome above problems of Array, we should go for “Collection” concept. 💡

  1. Collections are growable in nature i.e. based on our requirement we can increase/decrease the size.
  2. Collections can hold both homogeneous and heterogeneous elements.
  3. Every collection class is implemented based on some standard data structure. Hence for every requirement readymade method support is available.
Being a programmer, we are responsible to use those methods and we are not responsible to implement those methods.

What is the difference between Array and Collection ❓

Difference between Array & Collection

Collection

If we want to represent a group of individual objects as a single entity then we should go for “Collection”.

Collection Framework

It contains several classes and interfaces which can be used to represent a group of individual objects as a single entity.

The collection is already present in other languages but with different names. Take an example of C++ as below:

Collection alternative in C++

9 Key Interfaces of Collection Framework

1. Collection
2. List
3. Set
4. SortedSet
5. NavigableSet
6. Queue
7. Map
8. SortedMap
9. NavigableMap

Next, we will discuss the purpose of each interface one by on.

1. Collection

  • If we want to represent a group of individual objects as a single entity, then we should go for “Collection”.
  • Collection interface defines the most common methods which are applicable for any collection object.
  • In general, Collection is considered as root interface of Collection Framework.
  • There is no concrete class which implements Collection directly.

What is the difference between Collection & Collections ❓

Collection” is an interface. If we want to represent a group of individual objects as a single entity, then we should go for Collection.

Collections” is an utility class present in java.util package to define several utility methods for Collection objects (like sorting, searching, etc).

2. List

  • It is the child interface of Collection.
  • If we want to represent a group of individual objects as a single entity where duplicates are allowed, and insertion order must be preserved then we should go for “List”.
Implementations of List Interface

Note: In JDK 1.2, Vector and Stack class are re-engineered to implement List interface.

3. Set

  • It is child interface of Collection.
  • If we want to represent a group of individual objects as a single entity where duplicates are not allowed and insertion order is not required, then we should go for “Set”.
Implementations of Set interface

4. SortedSet

  • It is the child interface of Set.
  • If we want to represent a group of individual objects as a single entity where duplicates are not allowed and all objects should be inserted according to some sorting order, then we should go for SortedSet.

5. NavigableSet

  • It is the child interface of SortedSet.
  • It contains several methods for navigation purposes.
Implementations of SortedSet and NavigableSet interface

What is the difference between List and Set

Difference between List and Set

6. Queue

  • It is the child interface of Collection.
  • If we want to represent a group of individual objects prior to processing, then we should go for “Queue”.
  • Usually Queue follows “First In First Out” (FIFO) but, based on our requirement we can implement our own priority order also.

Example:

Before sending a mail, all mail id’s we must store in some data structure. In which order we added mail id’s in same order only mail should be delivered. For this requirement “Queue” is best choice.
Implementations of Queue interface

Note:

  • All the above interfaces (Collection, List, Set, SortedSet, NavigableSet and Queue) meant for representing a group of individual objects.
  • If we want to represent a group of objects as Key-Value pairs then we should go for “Map”.

7. Map

  • Map is not child interface of Collection.
  • If we want to represent a group of objects as Key-Value pairs then we should go for “Map”.

Example:

Student records stored by Serial number and Name

Note:

  • Both key and value are objects only.
  • Duplicate keys are not allowed but values can be duplicated.
Implementations of Map interface

8. SortedMap

  • It is the child interface of Map.
  • If we want to represent a group of objects as Key-Value pairs according to some sorting order of keys then we should go for “Map”.
  • In SortedMap, the sorting should be based on Key but not based on Value.

9. NavigableMap

  • It is the child interface of SortedMap.
  • It defines several methods for navigation purposes.
Implementations of SortedMap and NavigableMap interface

Complete Collection Hierarchy

Collection framework hierarchy

Note:
The following are legacy characters present in Collection framework:
1. Enumeration (I)
2. Dictionary (AC)
3. Vector (C)
4. Stack (C)
5. Hashtable (C)
6. Properties (C)

Availble helper interfaces and classes to use with Collection:

  1. Sorting:
    - Comparable (I)
    - Comparator (I)
  2. Cursors:
    - Enumeration (I)
    - Iterator (I)
    - ListIterator (I)
  3. Utility Classes:
    - Collections
    - Arrays

In this article, we discussed below points -

- Need of Collection Framework
- Collection Heirarchy
- Key interfaces in Collection Framework

Part 2 will be coming soon. Stay tuned!

Thank you for reading ❤️

--

--

Avinash Tingre
Javarevisited

Software Engineer. Jack of all trades; master of none :)