In this article, we will discuss Collection framework in detail covering all its related interfaces and core implementation classes
1. 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.
2. 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
3. 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
Source: Team BenchResources.Net
4. Core Collection interfaces:
- Collection
- List
- Set
- SortedSet
- NavigableSet
- Queue
- Map
- SortedMap
- NavigableMap
5. Core Collection implementation classes:
5.1 java.util.List
- ArrayList
- LinkedList
- Vector
- Stack
5.2 java.util.Set
- HashSet
- LinkedHashSet
- TreeSet
5.3 java.util.Queue
- PriorityQueue
5.4 java.util.Map
- HashMap
- LinkedHashMap
- IdentityHashMap
- WeakHasMap
- TreeMap
- Hastable
- Properties
Note: All core interfaces/implementation classes presents inside java.util package
6. 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:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
Happy Coding !!
Happy Learning !!