Translate

Saturday, November 24, 2012

COLLECTIONS FRAME WORK-01


COLLECTIONS FRAME WORK

Limitations of Object Arrays

An array is an indexed collection of fixed number of homogeneous data elements.

The main limitations of Object Arrays are

1) Arrays are fixed in size i.e once we created an array there is no chance of increasing or decreasing it’s size based on our requirement.
2) Arrays can hold only homogeneous data elements.

Ex:-

Student [] s = new Student [600];
s[0] = new Student;
s[1] = new Integer(10); X
s[2] = "raju"; X

We can resolve this problem by using object Arrays.

Object [] o = new Object [600];
o[0] = new Student;
o[1] = new Integer(10);
o[2] = "raju";

i.e By using Object Arrays we can hold heterogeneous data elements.

3) For the Arrays there is no underlying Data Structure i.e for every requirement we have to code explicitly and there is no default ready made support(like sorting, searching). we can’t represent array elements in some sorting order by default. We can’t prevent duplicate object insertion etc…




To resolve the above problems of arrays, Sun people has introduced ‘collections concept’

·        Collections are grow able in nature i.e based on requirement we can increase or decrease the size.

·        Collections can hold heterogeneous data elements also.

·        Every collection class has implemented based on some data structure

Hence for every requirement ready made method support is possible.
We can represent all the elements in some sorting order. We can prevent duplicate object insertion by default.

Comparison Between collections and Arrays

Collections
Arrays
1) Collections are not fixed In size.
1) Arrays are fixed In size.
2) With respect to memory collections are good.
2) with respect to memory arrays are not good.
3) With respect to performance collections shows worst performance.
3) With respect to performance the arrays are
recommended to use.
4) Collections can hold heterogeneous data elements.
4) Arrays can only hold homogeneous data elements.
5) Every Collection class has built by using some Data structure.
5) There is no underlying Data Structure.
6) Collection can hold only Objects but not primitives.
6) Arrays can hold both Objects and primitives.

Collections Frame Work

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

Java: Collection Frame Work collection

C++: STL(standard Template Library) container
7 key Interfaces of collection Frame Work

1)    Collection :
This interface is the root interface for entire collection framework.
This interface can be used for representing a group of objects as single entity.

This interface defines the most common general methods which can be applicable for any collection object.

There is no concrete class which implements collection interface directly.

Difference Between Collection and Collections
Collection is an interface available in java.util package for representing a group of objects as single entity.

Collections is an utility class available in java.util package and defines several utility methods for collection implemented class object

2)    List interface :

This can be used for representing a group of individual objects as a single entity where insertion order is preserved and duplicate objects allowed. This is child interface of collection.

The following classes implemented List interface directly.
ArrayList, LinkedList, Vector and Stack.


No comments: