Wednesday, 28 August 2013

Collection Framework



Difference Between Arrays and Collection Framework
Array stores element of same datatype but in collection framework we can value of any type.
The size of  array must be initialized at the time of declaration but in collection framework the size increases or decreases at run time.
      To know about Arrays

it is worked as a container which stores a group of objects.
All collection classes present in the java.util package.
it provides many interfaces and the classes which are as follows

Interface:-Set
implementation classes:-HashSet,LinkedHashSet

A Set represents a group of elements.it doesn't allow duplicate elements.no indexing.no key.
random selection of elements during output.

Interface:-List
implementation classes:-Stack,LinkedList,ArrayList,Vector.

List also allows a group of elements .it allows duplicate value.concept of indexing and keys occurs.

Interface:-Queue
first in first out.

Interface :-Maps
Implementation classes;-HashMap,HashTable.

it stores elements in key and value pairs.if the key is provided then its corresponding value can be obtained.
A key should have unique value.

In collections we can store only Objects and cannot store primitive datatype.

ArrayList():-dynamic allocation,contain duplicate elements,not synchronized,random access due to the indexing.present in java.util.ArrayList package.

Example:-Creation and printing of ArrayList




Example:-size() and get() method.


size():-returns the size of arraylist.
get().used to retrieve the value of arraylist.

Example:-add element at specified position.


it adds the element 10 at 2 location.

Example:-addAll();insert all elements of list1 into list2.



Example:-remove() and clear() method.
remove(true):-deletes true from the list.
clear():-clear the list.

Example:-removeAll():-it removes all the elements of list1 present in list2



Example:-retainAll():-it contains only those items in list1 which present in list2 also.


Example:-set() and contains() methods


set() method is used to update the value at specified position.
contains() method is used to check either the element present in the list or not.
return type boolean.

Example:-sorting in ascending or descending order.



for sorting we use collections and comparator classes.
Collections.sort():-sort the elements in ascending order.
Comparator();-sort the elements in descending order.

Vector:-vector is also same as the arraylist.the main difference between vector and arraylist is that vector is synchronized but arrraylist is not synchronized.and methods of vector same as the arraylist.it is present in the java.util.Vector package.

Example:-creation and printing of vector.


All the methods of vector is same as the arraylist.

LinkedList:-it is present in java.util.LinkedList package.

Example;-


the functionality of methods describes in the pic itself.these are the basic methods of the linked list.


PriorityQueue:- priorityqueue is known for the auto sorting.it takes elements in the sorted order.
when we are entering value in priority queue remember ,insert values of same datatype ,don't insert null values because when you insert the value automatically it apply the sorting process on this.
it does not sort whole queue ,the first element of the queue is sorted one

Example:-

 second SOP prints queue after deleting first element.see first element is sorted one not the whole queue.

HashSet;-.it doesn't allow duplicate elements.no indexing.no key. random selection of elements during output.not synchronized.it is present in java.util.HashSet package.

Example:-

it takes element of all datatype.
size();-returns the size of the hashset.

TreeSet:-it is used to sort the elements of hashset . Always remember 2 points during sorting nomatter in which class you performing sorting;-
all elements of same datatype.and no null elements present in the list.


it sorts the element of hashset.

HashMap TreeMap:-it stores elements in key and value pairs.if the key is provided then its corresponding value can be obtained. A key should have unique value.treeset is used to sort the elements of hashmap it sorts on the basis of key.

Example:-



Interfaces:-which is used to retrieve values from the list ,set and queue.

  1. Iterator
  2. Listiterator.

Iterator:-it is used to iterate the elements.it is present in java.util.Iterator package

Example:-

hasNext() method return type is boolean if next value is present then it return true otherwise false.
next() method return type is int.it prints the next element.

ListIterator:-it is also same as iterator but Listiterator provides more methods than iterator

Example:-



methods of listiterator:-

  1. boolean hasNext()
  2. boolean hasPrevious()
  3. element next()
  4. element previous()
  5. remove()
  6. void set()
  7. void add()

Difference between iterator and listiterator.

the main difference between iterator and listiterator is that in iterator we traverse the elements in forward direction only but in listiterator we can traverse the element in both direction.
in iterator we can read and remove the element but in listiterator we can read ,remove ,update, the elements.

Difference between ArrayList and Vector.

the main difference between Arraylist and vector is that vector is synchronized but arraylist is not synchronized.in case of single thread arraylist is more faster than vector and in case of multiple threads vector is more faster than arraylist.

Difference between HashMap and Hashtable

hashmap is not synchronized but hashtable is synchronized.hashmap allows null key and null values but hashtable doesnot allows null key and null values.

Difference between set and list.

set will not allow duplicate elements but list will allow duplicate elements.set will not allow null element but list will allow null elements

No comments :

Post a Comment