Site icon Narayana Tutorial

Collection Framework

Limitations of Object Array:-

⇒ Arrays are fixed in size i.e once we created an array there is no chance of increasing or decreasing the size our requirement i.e if we don’t know the size in advance, arrays are not recommended to use.

⇒ Arrays can hold only homogeneous data elements.

Student [] s = new student [ 100 ] ;

s [ 0 ] = new Student () ;

s [ 1 ] = new Customer () ; // compile time error, incompatible type

found : customer

required : student

⇒ But we can resolve this problem by using Object Array

Object [] a = new student [ 100 ] ;

a [ 0 ] = new Student () ;

a [ 1 ] = new Customer () ;

a [ 1 ] = new Teacher () ;

⇒ There is no underlying data structure for array. Hence ready made method support is not available. For every requirement compulsory we have to write code.

To resolve above problems, sun people introduced collections concept.

Arrays:-
  1. Arrays are fixed in size.
  2. Memory point of view, Arrays are not recommended to use.
  3. Performance point of view Arrays are recommended to used.
  4. Hold homogeneous elements.
  5. For every requirement no ready made methods are not available, because underlying data structure is not available.
  6. Arrays can hold both primitives & Objects.
Collections:-
  1. Growable in nature.
  2. Memory point of view, collections are recommended to use.
  3. Performance point of view Arrays are not recommended to used.
  4. Hold both homogeneous and Heterogeneous elements.
  5. Every collection class is implemented based on data structure & hence ready made method support is available for every requirement.
  6. Collections can hold both primitives & Objects.

 

Collection Framework

It define a group group of classes and interfaces which be used for representing a group of Objects as a single entity.

C + + (container) java (collection)
Standard template Library collection framework

Collection ( I )

Collection vs Collections

Collection is an interface which can be used for representing group of Objects as single entity. But collections as an utility class to define several utility methods for collection implemented Objects.