Collection And Collections Framework In Java

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:-

  1. 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.
  1. 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.
  1. 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”.

 

  1. 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.
  1. 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.

 

  1. 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’.
  1. 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.
  1. 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.
  1. 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 :
  1. Enumeration (I)
  2. Dictionary (ABC)
  • Vector
  1. Stack
  2. Hash table
  3. 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:
  1. Boolean add (object O)
  2. Boolean add All (collection C)
  • Boolean remove (object O)
  1. Boolean remove All (collection C)
  2. Boolean retain All (collection C)
  • To remove all objects except those present in collection
  1. Void Clear()
  • Boolean isEmpty()
  • Int Size()
  1. Boolean contains (object O)
  2. Boolean contains All (collection C)
  3. Object[] to Array()
  • Iterator iterator()

 

Leave a Reply

Your email address will not be published. Required fields are marked *