Collection:-
- A group of individual objects as a single entry is called Collection.
Collection Framework:
- It defined several Classes & Interfaces, which can be used to represent a group of objects as a single entity.
9-Key interfaces of Collections Framework are below:-
- Collection (Interface):
- If we want to represent a group of individual objects as a single entity then we should go for collection.
- In general collection interface is considered as root interface of Collection Framework.
- Collection Interface defines the most common methods which can be applicable for any collection object.
Collection Vs Collections:
- Collection is an interface, can be used to represent a group of individual objects as a single entity where as
- Collections is a Utility Class, present in java, util package, to define several utility methods for collections.
- List (Interface):
- It is the child Interface of collection.
- If we want to represent a group of individual objects where insertion order is preserved & duplicates are allowed. Then we should go for list.
- Vector & Stack classed are re-engineered in 1.2 version to fit into collection framework.
- Set (Interface):
- It is the child interface of collection.
- If we want to represent a group of individual objects where “duplicated are not allowed & insertion order is not preserved”. Then we should go for “Set”.
- Sorted Set (I):
- It is the child interface of Set.
- If we want to represent a group of individual objects according to some sorting order. Then we should go for Sorted Set.
- Navigable Set (I):
- It is the child interface of Sorted Set to provide several methods for Navigation purposes,
- It is introduced in 1.6 version.
- Queue (I):
- It is the child Interface of collection.
- If we want to represent a group of individual objects, prior to processing. Then we should go for Queue.
Note:
- All the above interfaces (Collection, List, Set, Sorted Set, Navigable Set, Queue) meant for representing a group of individual objects.
- If we want to represent a group of object as Key-value pairs then we should go for ‘Map’.
- Map (I):
- If we want to represent a group of objects as Key-value pairs. Then we should go for Map.
- Both key & value are objects only.
- Duplicate keys are not allowed. But values can be duplicated.
Note:
- Map is not child interface of collection.
- Sorted Map (I):
- If we want to represent a group of objects as Key value pairs according to some sorting order. Then we should go for Sorted Map.
- Sorting should be done only based on Keys, but not based on values.
- Sorted Map is child interface of Map.
- Navigable Map (I):
- It is the child interface of Sorted Map & define several methods for Navigation purposes.
- In the collection framework, following are Legacy characters :
- Enumeration (I)
- Dictionary (ABC)
- Vector
- Stack
- Hash table
- Propertied
Collection frame work:
Collection (I):
- If we want to represent a group of individual objects as a single entity then we should go for collection.
- Collection Interface defines the most common methods which can be applied for any collection object.
- Following is the list of methods present in collection interface:
- Boolean add (object O)
- Boolean add All (collection C)
- Boolean remove (object O)
- Boolean remove All (collection C)
- Boolean retain All (collection C)
- To remove all objects except those present in collection
- Void Clear()
- Boolean isEmpty()
- Int Size()
- Boolean contains (object O)
- Boolean contains All (collection C)
- Object[] to Array()
- Iterator iterator()