My first encounter with the collection framework was on my second day of Java programming. The collection framework has many implementations, and each of these is solving some very specific need of the data structure. There was a lot of information about every use case and its optimal collection implementations, but now, after four years, I still have to go through the same process when there's a decision to make about collection use.
Deciding which collection to use is often one of the most important decisions to make when designing a program. By definition, a collection provides means for storing and manipulating groups of data as a single unit. This single unit is called a collection -- it's sometimes also called a container.
- Value based containers
- Reference based containers
Value-Based ContainersValue-based containers store copies of objects. If we access an object, an object returns a copy of it. If external object is changed after it has been inserted in container, it will not affect the content of the container at all.
Reference-Based ContainersReference-based containers store pointers or references to the object. If we access an object, an object returns reference to it. If external object is changed after it has been inserted in container, it will also affect the content of the container.
Further to this, the container can be studied under three points of views.
AccessIt means accessing the container elements. In case of arrays accessing is done using array index. For stacks, access of elements is done using LIFO (Last In First Out) and in queues it is done using FIFO (First In First Out).
StorageIt includes storing of items of containers. Some containers are finite containers and some are infinite containers.
TraversalIt includes how the item can be traversed.
The SummaryNow, look for the collection with above concepts imprinted in brain. Selection of the correct container data structures in a program can do wonders.
- It can make the code a hundred times faster.
- Help in to reduce memory requirements
- To make the source code cleaner (in turn making it easier to write, debug, and expand).
Typically, the most commonly used data structures are ones which are implementations of abstract data types (containers). Whereas, an abstract data type (ADT) specifies what operations can be performed on a data structure. A data structure is the concrete implementation of ADT, specifying how much memory is required and, crucially, how fast the execution of each operation will be.