Saturday, January 13, 2018

Java collection framework in depth

Before going to discuss collection, first we need to discuss Array so that we can understand importance of Collection !!

An array is a indexed collection of fixed number of homogeneous data element.
The main advantage of array is we can represent multiple values by using single variable so that readablity of the code is improved.

Limitations of Arrays: 1. Arrays are fixed in size that is once we create an array there is no chance of increasing or decreasing the size base on our
requirement. Due to this to use array concept we should know the size in advance which may not possible always.
2. Array can hold only homogeneous data types elements.
Example:

Student[] s = new Student[100];
s[0] = new Student();
s[1] = new Customer();

This gives compile time error like:
incompatible types
found: Customer
required: Student


We can solve this problem by using object type arrays.

Object[] a = new Object[1000];
a[0] = new Student(); //valid
a[1] = new Customer();//valid

3. Arrays concept is not implemented based on some standard data structure and hence readymade method support is not support is not available.
For every requirement we have to write the code explicitly which increases complexity of the programming.

To overcome above problems of arrays we should go for collections concept.

1. Collections are growable in nature that is based on our requirement we can increase or decrease the size.
2. Collections can hold both homogeneous and heterogeneous objects.
3. Every collection class is implemented based on some standard data structure hence for every requirement ready made method support is available.
   Being a programmer we are responsible to use those method and we are not responsible to implement those method.

Collection: If we want to represent a group of individual object as a single entity then we should go for collection.

Collection Framework: It contains several classes and interfaces which can be used to represent a group of individual object as a single entity.

9 Key interfaces of Collection Framework:
1.Collection
2.List
3.Set
4.SortedSet
5.NavigableSet
6.Queue
7.Map
8.SortedMap
9.NavigableMap


1.Collection(I): 
a) If we want to represent a group of individual object as a single entity then we should go for collection.
b) Collection interface define the most common method which are applicable for any collection object.
c) In general, collection interface is considered as root interface of collection framework.

 There is no concrete class which implements collection interface directly.


Difference between Collection and Collections ?

Collection is an interface. If you want to represent a group of individual as a single entity then we should go for Collection.
Collections is an utility class present in java.util package to define several utility methods for collection objects (sorting,searching etc.).


List(I): It is the child interface of Collection. If you want to represent a group of individual objects as a single entity where duplicate are allowed and insertion order must be preserved then we should go for List.


Set(I): It is the child interface of collection. If we want to represent a group of individual object as a single entity where duplicate are not allowed and insertion order not required then we should go for Set Interface.

SortedSet(I): It is the child interface of Set. If you want represent a group of individual object as a single entity where duplicate are not allowed and all
object should be inserted according to some sorting order then we should go for SortedSet.

NavigableSet: It is the child interface of SortedSet interface. It contains several methods for navigation purposes.




Note: In red color text are classes.

Difference between List and Set ?





Queue(I): It is the child interface of collection. If you want to represent a group of individual object prior to processing then we should go for Queue.
Usually queue follows first in first out order but based on our requirement we can implement our own priority order also.
Example: Before sending a mail, all mail ids we have to store in some data structure. In which order, we added mail ids in the same order only mail should be delivered. For this requirement queue is best choice.



Note: All the above interfaces (Collection, List,Set, SortedSet, NavigableSet and Queue) representing a group of individual object. If you want to represent a group of as key-value pairs then we should go for Map.

Map(I): Map is not child interface of collection. If we want to represent a group of object as key value pairs then we should go for map. See example in below picture.


Note: Both key and value are object only. Duplicate key are not allowed but value can be duplicated.

SortedMap (I): It is 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 sorted map. In sorted map the sorting should be based on key but not based on value.

NavigableMap (I): It is child interface of sorted map. It define several methods for navigation purposes.

In Coming tutorial, We will discuss List, Set, Queue, Map in detail.


                                                                                                       
                                                                                                  List Interface with Example





No comments:

Post a Comment