Collection Framework : Class Hierarchy


Collection-hierarchy


Key Interface:IterableThis interface represents a collection of elements that can be iterated over, meaning you can access each element one by one. It is a base interface that enables the use of the for-each loop and other iteration constructs in Java. It contains the iterator(); the method that returns an Iterator<T> object and hasNext() and next() methods of Iterator can be used for traversal and retrieval of the objects.Top-level Interface extending Iterable:CollectionThis is the main interface of the framework. It defines fundamental operations that can be performed on any collection of objects, including adding, removing, and checking for the presence of elements.Sub-interfaces extending Collection:ListRepresents an ordered collection that allows duplicates. Elements can be accessed by their index. Implementations include: ArrayListLinkedListVector.SetRepresents a collection that doesn't allow duplicates. Implementations include: HashSetLinkedHashSetTreeSet.QueueRepresents a collection that follows a First-In-First-Out (FIFO) order. Elements are added to the rear and removed from the front. Implementations include: PriorityQueueLinkedList.DequeDeque extends the queue interface, it is a double-ended queue that allows adding and removing elements from both ends. Implemented by LinkedList , ArrayDeque .Separate Interface for Map:MapRepresents a collection of key-value pairs. It doesn’t extend Collection directly but is considered part of the framework. Implementations: HashMapTreeMapLinkedHashMapHashtable.Additional Interfaces:SortedSetA Set that maintains its elements in ascending order. Implemented by TreeSet.SortedMapA Map that maintains its keys in ascending order. Implemented by TreeMap.A Bit about ArrayDeque:This is a resizable array implementation of the Deque interface. Array deques have no capacity restrictions and grow as needed to accommodate usage.However, they are not thread-safe and do not support concurrent access by multiple threads without external synchronization.Null elements are prohibited.This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.Legacy Classes Which Are Not Part of the Modern Collection Framework:VectorAn early implementation of a List, now commonly replaced by ArrayList. It is thread-safe but less efficient for most use cases.StackA class for Last-In-First-Out (LIFO) operations, often used for managing function calls or undo/redo actions. Technically, it is a subclass of Vector, but its primary use is as a stack.HashtableAn early implementation of a Map, now generally replaced by HashMap. It is synchronized (thread-safe), but less efficient for most use cases.Integration with the Collection Framework:While legacy classes aren’t part of the core Collection Framework, they can interact with it in some ways:The Vector class implements a List, making it suitable for use in situations where a List is required.The Hashtable class implements Map, allowing it to be used in situations where a Map is expected.Although not a direct component of the framework, the Stack class can be adapted using a LinkedList to simulate stack-like behavior.Some Considerations That You Must Know:When writing new Java code, it is generally recommended to use the modern Collection Framework classes due to their improved performance and API design.However, legacy classes may still be encountered in older codebases or in specific scenarios where thread safety is a primary concern.It is helpful to understand the relationship between legacy classes and the Collection Framework when working with different Java codebases.Note that Interfaces define the behavior, while classes provide concrete implementations. We should select the appropriate collection based on our specific requirements. The framework provides a range of features for sorting, searching, and manipulating collections. So think twice while selecting a collection for storing elements.


Post a Comment

Previous Post Next Post