As we know, in order to store values in the Java programming language, we have a few options such as Variable, Array, and the Collection Framework. In earlier discussions, we explored how Variables and Arrays work. In this article, we will delve into how the Collection Framework assists us in storing data.
Before we delve into the Collection Framework, let's understand the restrictions imposed when using an Array to store data:
a. Arrays are static in nature, meaning that when we use an Array, we must declare its length, and we can't exceed this length.
b. Deleting a specific element at an index position from the array is challenging.
c. Arrays cannot avoid duplicity of elements.
d. Arrays always store data in the order they are added; to sort the elements, developers must manually write the logic.
To overcome the limitations of Arrays, the Collection Framework is introduced.
The initial Collection Framework consisted of five different classes, which are referred to as legacy classes:
a. Dictionary
b. Properties
c. Hashtable
d. Vector
e. Stack
These five classes addressed many issues with arrays, but they were completely synchronized, leading to an increase in application response time.
The Collection Framework was reengineered to address issues with legacy classes. Out of the legacy classes, two of them (Vector and Hashtable) were re-engineered and added to the new Collection Framework.
→ Collection is an interface available in the java.util package, further extended by java.util.List, java.util.Set, and java.util.Queue interfaces.
→ All the collections store elements in the form of objects.
→ List is an interface available in the java.util package, and it is further implemented by classes such as java.util.ArrayList, java.util.LinkedList, and java.util.Vector. All elements in a List-type collection can include duplicates.
→ Set is an interface available in the java.util package. It is implemented by classes such as java.util.HashSet and java.util.LinkedHashSet. It extends the java.util.SortedSet interface, and the SortedSet interface is implemented by the java.util.TreeSet class.
→ Queue is an interface in the java.util package, extended by the java.util.Deque interface. The Deque interface is further implemented by java.util.LinkedList and java.util.ArrayDeque. The java.util.Queue interface is implemented by the java.util.PriorityQueue class.