Recent Posts

Tuesday, 26 July 2016

Collection Framework Tutorial


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

Limitations of Object Arrays
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.
E.g
Student[] s = new Student[600];
s[0] = new Student; // Fine
s[1] = new Integer(10); //C.E
s[2] = "Ashok"; //C.E
     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] = "Ashok";
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’

Advantages of Collections over Arrays
1. Collections are growable in nature i.e., based on requirement we can increase or decrease the size.

2. Collections can hold heterogeneous data elements also.

3. Every collection class has implemented based on some data structure

Difference between Collections and Arrays

Collection 
     A group of individual objects as a single entity is called collection.

Collection framework
     It defines several classes and interfaces which can be used to represent a group of objects as a single entity.
     Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

What is framework in java
* Provides readymade architecture.
* Represents set of classes and interface.

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.

     Vector and stack classes are reengineered in 1.2 version to fit into collection frame work. By means of Indexes we can preserve insertion order and we can differentiate duplicate objects.

3. Set interface
     This can be used for representing a group of individual objects where duplicate objects are not allowed and insertion operation is not preserved.
     This is the child interface of collection hashset and linkedHashSet are the classes which implements Set interface directly.

4. SortedSet interface
     This can be used for representing a group of individual and unique objects. Where all the objects are inserted in some sorting order. It is the child interface of Set interface
TreeSet is the implemented class of SortedSet.

5. NavigableSet
* It is the child interface of SortedSet, to provide several methods for navigation purpose.
* It is introduced in 1.6 version

6. Queue
     It is the child interface of collection it has introduced in 1.5 version this interface can be used for representing a group of individual objects prior to processing.

     All the above interfaces(collection, List, Set, SortedSet, Queue) can be used for representing a group of individual objects. If u want to represent a group of objects as key value pair than we can’t use above interfaces. To handle this requirement sun people has introduced map interface.

7. Map
     This can be used for representing a group of objects as key value pairs. Both key and value are objects only.
StudentName  -> StudentRollNo
phoneNumber -> contactdetails
word                -> meaning
IP Address      -> Domain-name

     Map interface is not child interface of collection.
8. SortedMap
     It is the child interface of Map. If we want to represent a group of objects as key value pairs "according to some sorting order of keys" then we should go for SortedMap. Here Sorting should be done only based on keys. But not based on values. TreeMap is the class which implements SortedMap interface.

9. NavigableMap
     It is the child interface of SortedMap and defines several methods for navigation purposes.

Collection Vs Collections
     "Collection" is an "interface" which can be used to represent a group of objects as a single entity. Whereas "Collections" is an utility class" present in java.util package to define several utility methods for Collection objects.

Collection   --> interface
Collections  --> class

     In collection framework the following are legacy characters.
1. Enumeration(I)
2. Dictionary(AC)
3. Vector(C)
4. Stack(C)
5. Hashtable(C)
6. Properties(C)

Here I --> Interface, AC --> Abstract Class, C --> Class

Next Tutorial  Collection Framework Tutorial Part 2

No comments:

Post a Comment