Java Collection Framework(Part 01) — Overview

Damsak Bandara
5 min readMay 12, 2021

Introduction

A popular framework that provides an architecture to store and manipulate a group of objects. This framework has been introduced in JDK version 1.2 and contains all classes and interfaces.

There are 2 main root interfaces —

  1. The Collection interface ( java.util.Collection )
  2. The Map interface ( java.util.Map )

Before the introduction of the Collection framework, developers had to remember different methods, syntaxes, and various constructors present in different classes such as Arrays, Vectors, and Hashtables(classes that provide methods for grouping objects or collections before JDK 1.2). This was almost impossible and resulted in various problems. Developers had to write many different algorithms in order to do work with these early collections classes.

Advantages of Collection Framework

  • Reduce Programming effort.
  • Increases the speed and quality of the program.
  • Improve consistency.

Collection Framework Hierarchy

Collection Interface

  • This is the root interface for all the collections in the API and it is placed at the top of the collection hierarchy.
  • Provides basic operations for adding and removing elements.
  • This interface extends the iterable interface, which has 3 methods. They are iterator, spliterator (splitting + iterator), and foreach(Consumer<? super T> action). However, we can only implement the Iterator method. The other 2 methods have default implementations. This iterator method returns an iterator object and that object can be used to iterate over the available elements inside a collection.
  • List, queue, and Set interfaces extend the Collection Interface.

List Interface

  • Responsible for declaring the behavior of collections which stores a sequence of elements. A list may contain duplicate elements and the order is retained in which we add elements. That means we can retrieve elements according to this order.
  • There are 3 main subclasses that implement the list interface.
  1. ArrayList
  2. Vector
  3. LinkedList
  • The list defines some of its own methods as follows.
  1. Void add(int index, Object obj): Insert Object into the specified index.
  2. boolean addAll(int index, Collection c): Insert all elements of Collection c into the location of the specified index.
  3. Object get(int index): Return object stored at the specified index.
  4. int indexOf(Object obj): Return the index of the first instance of the object.
  5. int lastIndexOf(Object obj): Return the index of the last instance of the object.
  6. ListIterator listIterator(): Returns an iterator.
  7. ListInterator list iterator(int index): Returns an iterator that will invoke a list that begins at a specified index.
  8. Object remove(int index): Remove an element at the specified index.
  9. Object set(int index, object obj): Assign object to the location specified by the index.
  10. List subList(int start, int end): Returns list of elements from specified start to end.

Set Interface

Set is a special type of collection where we cannot store duplicate elements. This interface once contains the methods inherited from the Collection interface. This is an unordered collection of objects. Mathematical operations such as intersection, union, and difference are also supported in Set interface.

There are 2 main subclasses that implement the Set interface as it is.

  1. HashSet
  2. LinkedHashSet

TreeSet

This is implemented using the SortedSet interface. SortedSet is another special class that extends the Set interface. TreeSet uses a tree data structure to store the data.

SortedSet Behaves like a simple set with the exception that it stores elements in a sorted format.

Available Methods,

  1. add(element): All specific element to the set.
  2. addAll(collection) : Append all elements from the provided collection to the set.
  3. clear() : Remove all elements from the set.
  4. contains(element) : Check for a specific element in the set.
  5. containsAll(collection) : Check whether the set contains all elements present in the given set.
  6. hashCode() : Get the hashcode value.
  7. isEmpty() : Check whether the set is empty.
  8. iterator() : Return the iterator of the set.
  9. remove(element) : Remove given element from the set.
  10. removeAll(collection) : Remove all elements from a collection
  11. retainAll(collection) : Retain all elements from a set.
  12. size() : Get the size of the set.
  13. toArray() : Form an array using the elements in the set.

Queue Interface

Holds elements that are going to be processed in a special order called FIFO(First In First Out). In queues, we can insert elements from one end of the list and take out elements from the other end.

Queue Interface is implemented by a single class known as the PriorityQueue. However, this interface is extended by another interface call Deque which implemented the ArrayDeque class.

Deque

Dequeue stands for “double-ended queue”.In a double-ended queue, we can add or remove elements from either end of the queue. This increases the versatility of the queue. We can use the deque as a normal queue that follows FIFO. But most of the time deques are used to act as a stack that follows LIFO(Last In First Out). ArrayQue class implements the Deque interface.

Methods of Queue Interface

  • add(element): Add elements to the end of the queue.
  • element(): View the head of the element. ( Throw NoSuchElementException when the queue is empty)
  • offer(element): Insert an element into the queue. Returns false when the container is full.
  • peek(): View the head (top element) of the queue.
  • poll(): Removes and returns the head of the queue.
  • remove(): Removes and returns the head of the queue(Throw NoSuchElementException when the queue is empty).

Map Interface

Map interface behaves differently from the rest of the collection types. Map stores its elements as key/value pairs. Each object is mapped to a particular key. There cannot be any duplicate keys in Map. But we can have duplicate values.

The Map is ideal for searching, updating, and deleting records based on a key.

The next section of this article will describe more about these individual classes in the Collection framework and how to implement them.

--

--