Showing posts with label Collection. Show all posts
Showing posts with label Collection. Show all posts

Thursday, 28 November 2013

Difference between Array And ArrayList

Difference between Array And ArrayList



SR
Array
ArrayList
1
Array has a fixed length
it will grow as you ad elements to it
2
Array can store value  must be of similar data type  .
ArrayList value  may be differ it can store any object value
3
store value in contiguous memory location
store value in contiguous memory location
4
Array start with a index of 0
ArrayList start with a size of 0
5
In an array you have to track down yourself how many elements are in the array
you can always know how many elements contains an ArrayList by calling it's size() method
6
It does not Shrink
when you remove an element from an ArrayList it shrinks automatically

Iterator ListIterator and Enumeration


Iterator ListIterator and Enumeration



SR
Iterator
List Iterator
Enumeration
1
iterator was introduced after Enumerator

Enumeration is older and its there from JDK1.0
2
is Iterator has a remove() method
is ListIterator has a remove() method
while Enumeration doesn't.
3
Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist.

Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects,
4
Iterator is more secure and safe as compared to Enumeration because it  does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.
ListIterator is more secure and safe as compared to Enumeration because it  does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException
Less secure as compared to Iterator
5
Iterator interface methods:
hasNext()
next()
remove()
LisIterator hods:
hasNext()
next()
remove()
hasPrevious()
previous()
Enumeration
hasMoreElement()
nextElement()
N/A


6
We can iterate only in one direction .
We can iterate in both direction .
We can iterate only in one direction .
7
Iteration can be done only once. If you reach the end of series its done. If we need to iterate again we should get a new Iterator.
New ListIterator is not required
Enumeration can be done only once. If you reach the end of series its done. If we need to iterate again we should get a new E.numerator
8

The interface Java.util.ListIterator extends java.util.Iterator interface.

9
public class Test {

public static void main (String args[])
{
List<String> list =new  ArrayList();
list.add("01");
list.add("02");
list.add("03");
list.add("04");
Iterator LS= list.Iterator();

while (LS.hasNext()) {
String s=(String) LS.next();

System.out.println(s);
}


}
}


public class Test {

public static void main (String args[])
{
List<String> list =new  ArrayList();
list.add("01");
list.add("02");
list.add("03");
list.add("04");
ListIterator LS= list.listIterator();

while (LS.hasNext()) {
String s=(String) LS.next();

System.out.println(s);
}


}
}



public class Test {

public static void main (String args[])
{
List<String> list =new  ArrayList();
list.add("01");
list.add("02");
list.add("03");
list.add("04");
//get the Enumeration object
   Enumeration e = Collections.enumeration(list);
  
   //enumerate through the ArrayList elements
   System.out.println("Enumerating through Java ArrayList");
   while(e.hasMoreElements()){  System.out.println(e.nextElement());
   } }
}


Monday, 25 November 2013

Difference between Hashtable and ConcurrentHashMap and HashMap

Difference between Hashtable and ConcurrentHashMap and HashMap


HashMap
HashTable
ConcurrentHashMap
Not Synchronized
Synchronized
ConcurrentHashMap and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to caller
Hashmap allow one null key and Multiple null value
Not allow null keys or null values
ConcurrentHashMap do not allow null keys or null values
COLLECTION CLASS
LEGACY CLASSS
COLLECTION CLASS
Works in only Single thread models access
Once the size of hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.
Since ConcurrentHashMap introduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.
Not  Loked
Locked on Whole  Map
In Summary ConcurrentHashMap only locked certain portion of Map
You can make HashMap synchronized by wrapping it on Collections.synchornizedMap(HashMap) which will return a collection which is almost equivalent to Hashtable, where every modification operation on Map is locked on Map object

while in case of ConcurrentHashMap, thread-safety is achieved by dividing whole Map into different partition based upon Concurrency level and only locking particular portion instead of locking whole Map. by default  size is 16
HashMap only slightly better. in Single thread Environment
bad performance
ConcurrentHashMap is more scalable and performs better than Synchronized HashMap in multi-threaded environment
Collections.synchronizedMap and Collections.synchronizedList, provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applications  for example their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationException.


Does not maintain Insertion order
it maintain Insertion order





Reference :-


Sunday, 13 October 2013

Difference between ArrayList and Vector

Difference between ArrayList and Vector
Vector is Synchronized where as ArrayList is not Synchronized that means methods of Vector (add , remove , etc.. which modifies the vector structurally) are synchronized . If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it may lead to unexpected results.
2. Array List is faster comparing to Vector because methods of the ArrayList are not synchronized. So Array List is the better choice for single threaded environment.

3. When Vector crosses the current capacity, it doubles (100% increment) the capacity automatically, where as ArrayList increases its capacity almost by 50%. +1

Difference between ArrayList and LinkedList

Difference between ArrayList and LinkedList
1.      Since Array is an index based data-structure searching or retrieving element from Array with index is pretty fast. Array provides O(1) performance for get(index) method but remove is costly in ArrayList as you need to rearrange all elements. On the Other hand LinkedList doesn’t provide Random or index based access and you need to iterate over linked list to retrieve any element which is of order O(n).
2.      Insertions are easy and fast in LinkedList as compared to ArrayList because there is no need of resizing array and copying content to new array if array gets full which makes adding into ArrayList to amortized constant time of O(n) in worst case, while adding is O(1) operation in LinkedList in Java. ArrayList also needs to update its index if you insert something anywhere except at the end of array.
3.      Removal is like insertions better in LinkedList than ArrayList. LinkedList won’t need to re-arrange elements on removal.
4.      LinkedList has more memory overhead than ArrayList because in ArrayList each index only holds actual object (data) but in case of LinkedList each node holds both data and address of next and previous node.
5.      ArrayList should be used where you have more retrieval than insertions and deletions. while the LinkedList should be used when you have more insertions and deletions as it won’t need resizing on insertions and deletions.

Very Impotent Links for Java and Portal development development

Jar Download for Spring  Maven Or Gradel Dependency for Spring