A collection framework is a grouping of classes and interfaces that is used to store and manage the objects. To overcome the limitations of the arrays go to collections.
A collection is a group of individual objects. A java collection and collection framework is same as a C++ container and STL (Standard Template Library).
Limitations of arrays:
- Homogeneous elements
- No underlying data structure – therefore, no inbuilt methods are used.
- Growable in nature
- Store homogeneous or heterogeneous objects
- Implemented based on the standard data structure.
Key interfaces in java collection framework:
A collection interface defines the most common methods which are applicable for any collection object. A collection interface is considered as a root interface of collection framework.
Difference between collection and collections:
- A collection is an inteface which can be used to represent a group of individual objects as a single entity.
- A collections is a utility class present in java.util package used to define several utility methods (like sorting, searching…) for collection objects.
- List is a child interface of a collection.
- Where duplicates are allowed and insertion order preserved then we should go for list.
3. Set :
- Set is a child interface of collection.
- where duplicates are not allowed & insertion order not Preserved then we should go for the set.
Diffreence between list & set
|1||Duplicate are allowed||Duplicates are not allowed|
|2||Insertion oreder preserved||Insertion order not preserved|
- Child interface of set.
- If you want to represent a group of individuals objects as a single entity where duplicates are not allowed but all objects should be inserted according to some sorting order then we should go for shortedset.
- It is a child interface of sortedset if defines several methods for navigation purpose.
- It is a child interface of collection.
- If we want to represent a group of individual objects prior to processing then we should go for queue.
7. Map :
- Not child interface of collection.
- If we want to represent a group of individual objects as a key value pair then should go for map.
Both key and values are objects, duplicated keys are not allowed but values can be duplicated.
- It is a child interface of Map.
- If we want to represent a group of key value pairs according to some sorting order of keys then we should go for SortedMap.
- It is a child interface of Sorted Map.
- It defines several utility methods for navigation purpose.
If you want to learn more about collections in java then please see other articles related to the collection. Also, there are posts available related to java servlet.
I hope you enjoy this article! If you have any doubt then please feel free to comment me below and please share this site as much as possible I wanted to help others who love java programming.