Translate the content to your language. Select your Language below.

Friday, May 30, 2014

Collections Framework in Java


Collection is like a container which can holds group of elements.

Iterator:- is an interface which will loop a collection list and executes each object inside the list. It allows us to access collection in only forward direction.

ListIterator:- is also an Interface just like Iterator. The only diff is, it allows us to access collection either forward or backward direction

Enumeration:- This is also similar to Iterator but for read-only purpose. It doesnt have Remove() method.


List Interface:-Provides support for Ordered collections of objects.
| This may contain Duplicate elements.
|
|-- ArrayList:- Resizable Array implementation of List Interface.
| It can grow dynamically and it provides powerful insertion and search mechanisms than Arrays.
| This is not synchronized by Default.
| It can use only Iterator to access the elements.
| ArrayList increases its array size by 50% or 1.5 times when it runs out of room.
| It doesn't have any default size.
| During the Deletion, all elements in the Array have to be moved one step back to fill the space  created by deletion.
|
|-- Vector:-Synchronized resizable array implementation of List Interface.
| It is synchronized by Default.
| It can use either Iterator or Enumeration to access its elements.
| Vector increases its size 100% or 2 times when it runs out of room.
| It has a default size of 10.
|
|-- LinkedList:-Doubly linked list of the interface. May provide better performance than Arraylist if elements are frrequently inserted or deleted.
Data is stored in nodes that have reference to the previous node and the enxt node. So adding element is simple as creating the node and updating the next pointer on the previous node and previous pointer on the New node.
Deletion in LinkedList is fast because it invloves only updating the next pointer on the node before of the deleted node and previous pointer on the node after the deleted node.



Set Interface:- provides methods for accessing elements of a Set.
| Do not allow duplicate elements.
| Contains no methods otherthan the Inherited methods from Collection.
| It adds restriction that duplicate elements are prohibited.
| 2 SET objects are equal if they contain same elements.
|
|-- HashSet:- A HashSet is Unsorted and Unordered set.
| It uses hashcode of the object being inserted (So the more efficient your hasCode() implementation the better access performace you get).
| Use this class when user dont want Duplicates and not caring about order when it is iterated.
| It doesn't guarantee for either sorted order or sequence order.
| We can add any type of elements to HashSet.
|
|-- TreeSet:- Provides elements in the Sorted order [ascending].
| Elements are sorted according to the natural order of elements or by the comparataor provided at the time of creation.
| We can add only similar types of elements.
|
|-- L.HashSet:-
|
|-- EnumSet:- Specified set for use of eNum types.
All of the elements in this type that is specified explicitly or implicitly at the time created.



Map Interface:- A Map is an Object which stores associations between Keys and Values.
| Both Keys and Values are objects.
| Keys must be unique and values can be duplicated.
| Some Maps can accept Null keys and Null Values, other maps not allow.
|
|-- HashMap:- For inserting, deleting and locating elements in a Map, this is the best alternative.
| Depending upon the size of your collection, it may be faster to add elements in the HashMap and later convert it to TreeMap for better Sorted Order key travesal.
| Allows us to have all NULL values and one NULL key.
| Iterator in the HashMap is fail-safe [if you cahnge the map, while iterating we can see].
| It is UnSynchronized.
|
|-- HashTable:- Doesn't allow NULL values as KEY or Value.
| Enumerator for the HashTable is NOT a fail-safe.
| It is Synchronized.
|
|-- TreeMap:- Implements SortedMap interface which extends internally Map interface.
| Data will be sorted in the ascending order of the keys according to natural order of key's class or by the comparator provided at runtime.
| When we need to traverse the keys in a sorted order then TreeMap is the better choice.
| Depending on the size of your collection, it may be faster to add elements in the HashMap and later convert it to TreeMap for better Sorted Order key traversal.
| TreeMap is based on the Red-Black tree data structure.
|
|-- EnumMap:-  

Maps provide 3 different collection views.

1.) Key Set View :- is a Set returned by keySet() method of Map Interface. It contains all the keys in the map.

2.) Values Collection View:- is a collection returned by values() method of Map Interface. It contains all objects as Values in Map.

3.) Entry Set View:- is a set returned by entryset() method of Map Interface and contains Objects of type map. Entry each of which has both Keys and Values.


No comments:

Post a Comment