Tuesday, 5 June 2012

Java Collections

Collection: Java collection is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. 


Benefits of Java Collection framework:
1. Reduces programming efforts.
2. Increase programming speed and quality. 
3. Reduces efforts to design new APIs.
4. Standard collections interface are reusable.


Interfaces:


===========================================================
Collection Interface : 
This interface is root of collection hierarchy. Java platform does't provide any direct implementation of the this interface but provides implementations of more specific subinterfaces, such as Set, List and Queue.  Collection interface is generic. A Collection represents group of objects, known as its elements.


Interface declaration :
public interface Collection<E> extends Iterable<E>


Known Exception : UnsupportedOperationException
===========================================================
Set Interface : 
This interface implements Collections interface. In this type of collections no duplicate values are allowed.  


Interface declaration :
public interface Set <E> extends Collection <E>


Features : 
1. Set does not allow duplicate elements.
2. Set contains at most one null element.
3. Set is unordered collection.
4. Set does not allow positional access.


Known Exception :NullPointerException or ClassCastException
----------------------------------------------------------------------------------------------------------
General purpose Implementation classes of Set Interface are :
1. HashSet :
This class implements the Set interface backed by Hash table.
Class declaration :
public class HastSet <E> extends AbstractSet <E>
implements Set <E>, Cloneable, Serializable
Features : 








1. HashSet does not allow duplicate elements.
2. HashSet contains at most one null element.
3. HashSet is unordered collection.
4. HashSet does not allow positional access.
5. Unsychronized collection set.

HashSet can be converted into a synchronized collection. 
ex.

Set s = Collections.synchronizedSet(new HashSet(.........));
----------------------------------------------------------------------------------------------------------
2. LinkedHashSet :
This class implements the Set interface and extends to HashSet. It maintains the order in which the elements were inserted in this set.

Class declaration :
public class LinkedHashSet <E> extends HashSet <E>
implements Set <E>, Cloneable, Serializable

Features :
It carries all the features of HashSet except the ordering of collection maintained by the LinkedHashSet is the order in which the elements were inserted into it.

HashSet can be converted into a synchronized collection. 
ex.

Set s = Collections.synchronizedSet(new LinkedHashSet(.........));
===========================================================
SortedSet interface :
This interface extends to set interface. This set guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements.

Class declaration :
public class SortedSet <E> extends Set <E>

Features :
1. Ordered collection set.
2. Does not allow duplicate elements.
3. All element inserted into an sorted set must implement Comparable interface.
----------------------------------------------------------------------------------------------------------
General purpose Implementation classes of SortedSet Interface are :
1. TreeSet :
This set implements SortedSet interface. 
Class declaration :
public class TreeSet <E> extends AbstractSet <E>
implements SortedSet <E>, Cloneable, Serializable
Features :
Carries all the features of the SortedSet interface and this class is unsynchronized.

Can be converted to synchronized collection.
ex.
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));

===========================================================
List Interface : 
This interface implements Collections interface. An ordered Collection also known as sequence.








Interface declaration :
public interface List <E> extends Collection <E>








Features : 
1. List allows duplicate elements.
2. List contains null elements.
3. List is a ordered collection.
4. Allows positional access.
5.List interface provides specialized Iterator called ListIterator that allows insertion, replacement and bidirectional access in addition to the normal operations that the iterator interface provides.

Known Exception :NullPointerException or ClassCastException
----------------------------------------------------------------------------------------------------------
General purpose Implementation classes of SortedSet Interface are :
1. ArrayList :

This ArrayList implements List interface. 

Class declaration :
public class ArrayList <E> extends AbstractList <E>
implements List <E>, RandomAccess, Cloneable, Serializable

Features :
1. Resizable-array implementation of the List interface.
2. Permits duplicate elements.
3. Both LinkedList and the ArrayList maintain the order in which the elements are inserted.
4. Roughly equivalent to Vector except that it is unsynchronized.
5.The iterators returned by this class's iterator and listIterator methods are fail-fast.

Can be converted to synchronized collection.
ex.
List list = Collections.synchronizedList(new ArrayList(...));
----------------------------------------------------------------------------------------------------------
2. LinkedList :
Doubly-linked list implementation of List interface. In addition to implementing the List interface the linkedList class provides uniformly named methods to get, remove and insert element at the beginning and end of the list. These operations allow linked lists to be use as a stack, queue, or double-ended queue(deque).Provide better performance if the elements are frequently inserted or deleted within the list. Also implements Queue interface. When accessed via the queue interface, LinkedList behaves as FIFO (First In First Out) Queue.
Class declaration :
public class LinkedList <E> extends AbstractSequentialList <E>
implements List <E>, Queue <E>, Cloneable, Serializable
Features :
1. Resizable-array implementation of the List interface.
2. Permits duplicate elements.
3. Both LinkedList and the ArrayList maintain the order in which the elements are inserted.
4. All of the operations perform as could be expected for a doubly-linked list
5. Unsynchronized list.
Can be converted to synchronized collection.
ex.
List list = Collections.synchronizedList(new LinkedList(...));

===========================================================
Map interface : 
An object which maps keys to value. A map can not contain duplicate values and each key can map to only one value.



Class declaration :
public interface Map <K,V>

Features :
1. Object maps key and value.
2. Map cannot contain duplicate key.
3. Each key can map at most one value.
4. Map at most contain one null key value.
5. Unordered collection.

Exception : NullPointerException, ClassCastException or UnsupportedOpertionException







----------------------------------------------------------------------------------------------------------

General purpose Implementation classes of Map Interface are :
1. HashMap :
Hash table based implementation of the Map interface.
Class declaration :
public class HashMap <K,V> extends AbstractMap <K,V>
implements Map <K,V>, Cloneable, Serializable
Features : 
1. Unsynchronized map.
2. Unordered map.
3. Allows null key and value at most one.
4. HashMap is roughly equivalent to the Hash table except that it is unsynchronized and allows null.
HashMap can be converted into a synchronized map. 
ex.
Map m = Collections.synchronizedMap(new HashMap(...));
----------------------------------------------------------------------------------------------------------

2. LinkedHashMap :

Hash table and linked list implementation of the Map interface,
 with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through
 all of its entries.  This linked list defines the iteration ordering,
 which is normally the order in which keys were inserted into the map
 (insertion-order).  Note that insertion order is not affected
 if a key is re-inserted into the map.

Class declaration :
public class LinkedHashMap <K,V> extends HashMap <K,V>
implements Map <K,V>
Features : 
1. Unsynchronized map.
2. Maintain the insertion order.
LinkedHashMap can be converted into a synchronized map. 
ex.
Map m = Collections.synchronizedMap(new LinkedHashMap(...));
===========================================================

SortedMap interface : 

A map whose mapping are automatically sorted by key, either in keys natural ordering by a compartor provided when a sortedMap instance is created. It extends Map interface.

Interface declaration :
public interface SortedMap <K,V> extends Map <K,V>
Features :
1. Sorted Key in ascending.
2. EntrySet, KeySet for iterating over map
----------------------------------------------------------------------------------------------------------
General purpose Implementation classes of SortedMap Interface is :

TreeMap :

Implementation of SortedMap interface.

Class declaration :
public class TreeMap <K,V> extends AbstractMap <K,V>
implements SortedMap <K,V>, Cloneable, Serializable
Features : 
1. ascending key order.
2. Iterator are fail-safe.
===========================================================

Legacy Implementations - Older collection classes have
been retrofitted to implement the collection interfaces.

Vector :
Synchronized resizable-array implementation of the List interface
with additional "legacy methods." 

 
Class declaration :
public class Vector <E> extends AbstractList <E>
implements List <E>, RandomAccess, Cloneable, Serializable
Features : 
1. Synchronized resizable array implementation od List interface with additional Legacy methods.
2. Size of vector can grow or shrink as the needed to accommodate adding and removing items after the vector has been created.
----------------------------------------------------------------------------------------------------------

Hashtable :
Synchronized hash table implementation of the Map interface
that does not allow null keys or values, with additional
"legacy methods."

Class declaration :
public class Hashtable <K,V> extends Dictionary <K,V> 
implements Map<K,V>, Cloneable , Serializable

Features:
1. Synchronized Map.
2. Does not allow null values for key and value.
===========================================================

Difference between HashMap & HashTable

1. HashTable is synchronized while HashMap is't.
2. Iterator in HashMap is fail-safe while enumerator in Hashtable is't.
3. HashMap permits null value in it but Hashtable does't.


Difference between ArrayList & LinkedList

1. ArrayList can Random access elements in the list so its faster while accessing, even though the LinkedList has the method get(int index), the LinkedList has to iterate over the list to get the index value so its slower as compared to ArrayList while doing random access.
2. LinkedList is faster while doing the delete and add operations than ArrayList.

Reference of the Collection

No comments:

Post a Comment