Collection Framework
Introduction :
The java collection framework standardizes the way in which groups of objects are handled your programs. Before to java2 we had five legacy classes. They are
1.Dictionary
2.Vector
3.Stack
4.Hash Table
5.Properties
Among these, Vector and Stack are used to store set of objects. Dictionary, Hashtable and Property classes are used to store Key-value pairs.
And we had one legacy interface called Enumeration which is used to visit the elements of legacy class.All the legacy classes are synchronized bydefault.When object is synchronized only one thread is allowed to access that object at a time which results "system will slow and it will take moreime".To avoid this problems Sun has introduced Collection Framework from java2.
Hierarchy Structure of Collection Framework :
Collection Framework has three categories.
1.List
2.Set
3.Map
» List is used to store collection of objects.
» List allows duplicates.
» Set also used to store collection of objects.
» Set does not allow duplicates.
» Map is used to store key-value pairs.
ArrayList :
» ArrayList class is sub class of list which is implementing marker interfaces called random access clonable and serializable.
» ArrayList is used to store group of objects similar or dissimilar objects.
» ArrayList allows duplicates.
» Objects in the array list will be store internally using index notation.
» ArrayList is not synchronized by default because it is a collection class.
Vector :
» Vector is a sub class of list whose functionality is very similar to array list.
» Sun has reengineered vector has a sub class of list to make use of functionality of list and collection interfaces.
» The main difference between vector and array list is "vector is synchronized by default where as array list is not synchronized by
default".
Difference between ArrayList and Vector List :
ArrayList
|
Vector List
|
1.ArrayList is not synchronized by default.
|
1.Vector List is synchronized by default.
|
2.ArrayList can use only Iterator to access
the elements. |
2.Vector list can use Iterator and Enumeration
Interface to access the elements. |
3.ArrayList is implementing Random Access
marker interface because of this we can pick the elements |
3.Vector list is implements random access
marker interface. Because of this we can pickup the elements randomly from vector |
4.JVM using Index notation to store the array
list internally. |
4.JVM will use Index notation to store the
vector list internally. |
Vector :
» Set is a collection of interface which does not allow duplicates.
» This interface has three sub classes.
1.Hash Set
2.Tree Set
3.Linked Hash Set
Difference between Hash set and Tree Set :
HashSet
|
TreeSet
|
1.HashSet is under set interface i.e. it
does not guarantee for either sorted order or sequence order. |
1.TreeSet is under set i.e. it
provides elements in a sorted order (acceding order). |
2.We can add any type of elements
to hash set. |
2.We can add only similar types
of elements to tree set. |
Map Interface :
» Map interface is used to store key-value pairs.
» Map is a part of collection framework but it not under collection interface.
Sub-class of Map :
1.Hash Map
2.Hashtable
3.TreeMap
4.LinkedHashMap
HashMap :
» HashMap is an unordered set.
» HashMap does not allow duplicate keys and is allow duplicate values.
» HashMap will allow null keys and null values.
Hash Table :
» Hashtable is a legacy class which was recognized as a subclass of map interface.
» Hashtable does not allow null values.
HashMap
|
HashTable
|
1.HashMap is not synchronized by default.
|
1.Hashtable is synchronized by default.
|
2.HashMap allows null keys.
|
2.Hashtable does not allow null keys.
|
3.HashMap allows null values
|
3.Hashtable does not allow null values.
|
Similarities of Hash map and hash table :
» Both HashMap and Hashtable does not allows duplicate keys but both allow duplicate values.
» Both HashMap and Hashtable are unordered maps.
TreeMap :
» TreeMap is a sorted map i.e. which gives the entries in sorted order using keys.
» Null keys are not allowed.
» Duplicate keys are not allowed.
» Null values are allowed.
» Duplicate values are allowed.
No comments:
Post a Comment