Friday 15 December 2017

Difference: Array vs Vector in java

Array and Vector differs based on the following parameters:

Basic:

Array in java is an object that contains data of homogeneous (similar) type.
example, int a[]={5,6,4,55}; //this array can only store int data type

Vector can store heterogeneous data types, if restriction is not put using generics concept. See the example below:

package string;

import java.util.Iterator;
import java.util.Vector;

public class splitDemo {

      public static void main(String[] args) {

            Vector v = new Vector();
            v.add(1);    //integer
            v.addElement("xyz");  //string
            v.add('s');  //char

            Iterator itr = v.iterator();
            while(itr.hasNext()){
                  System.out.println(itr.next());
            }
      }
}

OUTPUT:
1
xyz

In above example, you can see we have stored int, String and char objects in vector. 

Note: To make vector store homogeneous data type do the following:

Vector<String> v = new Vector<String>();

This will only allow storage of String object in vector.

Family:

Array does not belongs to Collection family. It is a primitive data type.

Vector implements List interface which is a part of collection hierarchy. Thus vector is a part of collection family.

Size:

Array needs to be assigned with the fixed size during initialization thus making it static in nature.

Vector resizes itself dynamically as per the need. Thus it is dynamic in nature.

Synchronized:

Array is non-synchronized in java.

Vector is synchronized in java.

Traversal:

Array can be traversed using for loop or enhanced for loop. It cannot use iterator or enumeration interface like vector or arraylist.

Vector can be traversed using iterator or enumeration interface

Declare/Instantiate/Initialization:

Array
Integer array
int a[]={5,6,4,55};

String array
String a[]={"Ajay","vijay","Rajesh","Mukesh"};

Similarly you can do for other data types like double, float, char, etc

Vector
Vector<String> vec=new Vector<String>();  //create vector
vec.add("Ajay");      //add element to vector
vec.add("vijay");




You may also like to read:

Wednesday 13 December 2017

Difference: ArrayList vs CopyOnWriteArrayList in java

ArrayList and CopyOnWriteArrayList in java differ based on following parameters:

1. Inclusion in java

Arraylist was added in Java 1.2 version.

CopyOnWriteArrayList was added in Java 1.5 or Java 5.


2. Thread-safety

Arraylist in java is not synchronized thus is not at all thread safe.

CopyOnWriteArrayList is synchronized in nature. Thus it is thread safe and for the same reason only one thread can access the resources in this class at a time.


3. Performance

As arraylist is non-synchronized so it is better performance wise as multiple threads can access the resources of arraylist at the same time simultaneously.

Being synchronized means only one thread can have access to CopyOnWriteArrayList resources thus impacting its performance speed.


4. ConcurrentModificationException

Arraylist throws ConcurrentModificationException.

CopyOnWriteArrayList doesnot throw ConcurrentModificationException.


5. Fail fast or fail safe

 Arraylist's  iterator and list iterator are fail fast in nature.

CopyOnWriteArrayList iterator are fail safe in nature.



You may also like to read:


Tuesday 12 December 2017

Which collection classes are synchronized or thread-safe?

Collection framework provides lots of classes to work with. Among them some are synchronized while other are non synchronized.

Recently one of my colleague was asked the same question in Deloitte interview. So you could also be asked the same in your next interview.

Lets come to the point.


All the synchronized collection classes are stated below:


1. Vector


Vector implements grow able array of objects. The elements in vector can be accessed using integer index just as we do in array.

Vector is synchronized so it should be used if thread-safety is concern of the developer. 
Due to synchronized nature, it does not allow multiple threads to access and modify its resources concurrently. If one thread has access to the vector then other thread wanting to access and operate on it must wait for its turn until the vector is released by the thread in action.

On other hand, this synchronized nature affects Vector’s performanceSo ArrayList is preferred over Vector if performance is to be considering factor.

To read about Vector in details. please follow the link Vector in java 

2. Hashtable

Hashtable extends Dictionary class and implements Map interface. Hashtable stores elements in key-value pair.

Hashtable is synchronized in nature.
Due to synchronized nature, it does not allow multiple threads to access and modify itself concurrently. 


Note: Both vector and hashtable are legacy classes and are not recommended for use.

3. CopyOnWriteArrayList

CopyOnWriteArrayList is a thread-safe variant of ArrayList which was included in java version 1.5 or java 5.
Here thread-safe indicates that this collection class is synchronized.



Advantage of synchronization:
If you don't want any resource to be shared simultaneously by multiple threads then mark the resource Synchronized. This makes sure that only one thread can access the resource at one instance and others has to wait for their turn in the queue.


Disadvantage of synchronization:
Because of being synchronized in nature these classes are slow in terms of performance. Reason is simple. Because of synchronization only single thread is able to access the class resource at a time. Other thread has to wait in queue till the first thread completes its task on the resource. Thus multitasking is not possible which in turn impacts speed.



You may also like to read:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.