Java Collection Framework(Part 01) — Overview

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.

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

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. Vector
  2. LinkedList
  1. boolean addAll(int index, Collection c): Insert all elements of Collection c into the location of the specified index.
  2. Object get(int index): Return object stored at the specified index.
  3. int indexOf(Object obj): Return the index of the first instance of the object.
  4. int lastIndexOf(Object obj): Return the index of the last instance of the object.
  5. ListIterator listIterator(): Returns an iterator.
  6. ListInterator list iterator(int index): Returns an iterator that will invoke a list that begins at a specified index.
  7. Object remove(int index): Remove an element at the specified index.
  8. Object set(int index, object obj): Assign object to the location specified by the index.
  9. 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.

  1. LinkedHashSet
  1. addAll(collection) : Append all elements from the provided collection to the set.
  2. clear() : Remove all elements from the set.
  3. contains(element) : Check for a specific element in the set.
  4. containsAll(collection) : Check whether the set contains all elements present in the given set.
  5. hashCode() : Get the hashcode value.
  6. isEmpty() : Check whether the set is empty.
  7. iterator() : Return the iterator of the set.
  8. remove(element) : Remove given element from the set.
  9. removeAll(collection) : Remove all elements from a collection
  10. retainAll(collection) : Retain all elements from a set.
  11. size() : Get the size of the set.
  12. 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.

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

Associate Software Engineer at Virtusa

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store