Collection framework in java

Need of collection framework

Aditya Kumar
4 min readAug 29, 2021

Why do we need a collection framework in java?.

Before the collection framework was introduced, arrays, vectors, and the hash table were used to store data and perform add, search, sort, and delete operations on them. Following are differences between collection and array.

To overcome the above problems with array collection framework was introduced in the Java 1.2 version.

Hierarchy of collection framework.

The map is the part of java collection framework but it is not part of Iterable and Collection interface because map store key-value pair while other are a group of an individual item.

Which collection interface and classes should we use.

It depends on the need of the project. However, the following points should be kept in mind while choosing the interface and classes.

  1. Order: sometimes we need order and sometimes we don't need items to be stored in a specific order.
  2. Duplicates: Sometimes it is okay to have duplicates and sometimes duplicates are strictly prohibited.
  3. Speed of Operation: We perform different operations on data. Some algorithms are fast at searching data, some are good at adding data.
  4. Memory: Some collections consume high memory, some consume less memory. we should keep in mind while choosing the collection.

List implementation.

  1. ArrayList: It is used to implement a dynamic version of the array. It supports data searching and manipulation by index. Duplicates are allowed in the ArrayList.
  2. CopyOnWriteArrayList: This is synchronized version of arraylist.
  3. LinkedList: This is the implementation of the doubly linked list data structure.In which each node stores data and a reference to the next and previous node.LinkedList is very good if we want deletion, insertion of an element in the middle of the list.
  4. Vector: Vector class is dynamic array implementation of the array, the only difference is this is thread-safe.
  5. Stack: Stack in java is the child class of Vector. This is based on the principle of last-in and first-out (LIFO).

LinkedList vs ArrayList:

Deletion in LinkedList is fast because it just fixes the link whereas deletion in ArrayList takes linear time because every element is shifted to the left. So we should use LinkedList when deletion operation is frequent. We should use ArrayList when insertion and searching by the index are frequent.

Queue and Deque implementation

The queue is based on the first-in and first-out principle. Queue and Deque interfaces are implemented by LinkedList and ArrayDeque class. Another type of queue that stores elements on some priority are called a priority queue. If a smaller number is given more priority then it is called a min priority queue.

Set implementation.

Set is a collection of unique elements. Duplicates are not allowed in Set. In java by default, elements are not ordered. In TreeSet elements are stored in ascending order. The set interface does not introduce any method, all methods are inherited from the Collection interface.

Map implementation in java.

A map is used to store the key-value pair. The map is not the child interface of Iterable. However, it is a part of the collection framework. If we don’t want ordering then use initialize the map with HashMap. If we want elements to be sorted on key we use TreeMap.

Comparator and Comparable.

Both are interfaces used for sorting the user-defined data type(class). We use Comparable for default order sorting and Comparator for any custom sort order. Thus Comparator is more flexible than Comparable.

Iterable, Iterator and ListIterator.

Iterable is a collection of elements that can be iterated over. This has one method which produces iterator.

Iterator is a pointer for traversing in the data structure.

ListIterator is a bi-directional iterator that traverses list kind of collection.

--

--

Aditya Kumar

Hi, I am Aditya. I studied at IIT BHU Varanasi , India. I am a Java developer. I partricipate in competitive programming.