Collection framework

In this article, we will discuss Collection framework in detail covering all its related interfaces and core implementation classes

Collection:

  • A collection is a group of element/objects represented as single unit/entity
  • On which programmers can perform various operations
  • Like insertion, deletion, sorting, searching, and reversing, etc.

Why we need Collection framework ?

  • In Java 1.0 version, there are several classes to achieve above mentioned concepts, but they were all arranged in ad-hoc basis
  • These classes are Vector, Stack, Dictionary, Hashtable and Properties
  • These classes collectively referred as legacy classes and every method inside legacy classes is synchronized (i.e.; Thread-safe access)
  • With Java 1.2 version introduction, Sun (now Oracle) group came up with Collection framework putting together above legacy classes and newly introduced classes into one place (i.e.; java.util package) under root-interface java.util.Collection

Advantages of Collection framework:

  • Used to store group of objects as a single unit/entity
  • Dynamically grow-able in nature i.e.; collection size increases as more number of objects added and size shrinks when deleted
  • Prior to Java 1.5 version introduction, collection allows to store/save any type of objects
  • With the introduction of generics in Java 1.5 version, collection are type-bounded (i.e.; within angle brackets, programmer can define type of objects it allows to be added)
  • Every collection class is based on some standard data-structures like for example, dynamic-array for ArrayList/Vector classes and hashtable for HashSet, etc
  • There are standard ready-made API’s available to operate on Collection like adding/deleting elements, etc
  • When programmer performs any operation on Collection classes, then it works based on the algorithm of the underlying data-structures
  • It has several interfaces/classes to operate on group of element/objects

Figure: Collection framework hierarchy

01-collection-framework-hierarchy-in-java

Source: Team BenchResources.Net

Core Collection interfaces:

  • Collection
  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Map
  • SortedMap
  • NavigableMap

Core Collection implementation classes:

java.util.List

  • ArrayList
  • LinkedList
  • Vector
  • Stack

java.util.Set

  • HashSet
  • LinkedHashSet
  • TreeSet

java.util.Queue

  • PriorityQueue

java.util.Map

  • HashMap
  • LinkedHashMap
  • IdentityHashMap
  • WeakHasMap
  • TreeMap
  • Hastable
  • Properties

Note: All core interfaces/implementation classes presents inside java.util package

Misconception about Map:

  • Although we are discussing Map under collection framework
  • map neither inherit any properties/methods nor implement any interfaces of collection
  • Since map deals with group of key-value pairs, which is represented as a single unit/entity is always discussed under collection framework

 

References:

 

Happy Coding !!
Happy Learning !!

Collection interface
ArrayList v/s LinkedList