java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework.
All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized. All the new implementations of java collection framework is not synchronized.
Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.
Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.
Vector or ArrayList? Which is better to use in java?
In general, executing a ‘synchronized’ method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to be used.
Is there an alternate available in java for Vector?
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.
When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.
When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a costlier operation.
Comments are closed for "Difference between Vector and ArrayList in java?".
Good…Keep it up
Hi Joe,
Your explanation is great. Wondering if you can provide more info on Java Collections Framework and when to use a particular Collection implementation.
the diference i read it’s great
could u just inform what is the default load factor of ArrayList and Vector
You are not talking about initial capacity when ArrayList or Vector gets created.
The default size for Vector & ArrayList is 10
hey thats great. But will u please tell me more about thread safe methods.
hi can you explain me how to iterate through a map
in java.
excellent
good notes on difference between vector and arraylist……
excellent theory , but its good if it has programmatic explanation also for better understand….
its great answer
this is great answering for diffrent between ArrayList and Vector
keep it ! up
ArrayList has a default size?
i came to know that only vector has it?
can you please explain me in detail
ArrayList has a default Size is 0
Vector has a default Size is 10
Vector is synchronized and ArrayList is not synchronized.
Very nice article
Its excellent . Exactly what I was looking for. Thank you.
Sir
Thanks for the great work u are doing. I am just confused on the terms synchronized and non-synchronized , o could you please evaluate these two terms with reference to above article.
Thanks
Regards
Umair
Crispy explanation
also explan about vector size and arraylist pls do needfull
Very good explanation so i have cleared with my doubt i would like to thanks to such a great info could you tell about synchronous and non-sysnchronous methods in vector and arraylist
warm regards
surender reddy
impressive work!!
Hi joe,
this is good but i need more deeply can u provide it ??
hey you explain collection framework concept nicely….
be continue
can any one explain enum in details with example
excellent, good , superb
your blog is very usefyl and informative. Your explanation is clear and very easy to understand.
It’s Good Article.Thank You.
great……..
Good way to understood the difference b/w them. Keep it up for good work.
NICE EXPLANATION SIR
Nice Explanation but it will add cherry on cake if u explain how vector is Synchronized and how ArrayList is not-Synchronized with An Example.And Multiple Thread can Access vector And ArrayList.
Your explanation is great, I am expecting discuss more in java and post this site.
thanks joe i think it will help me
Why do we need Vector if we can have synchronized ArrayList? Which one is better Vector or synchronized ArrayList?
Hi Sir:
can you able to give me some tips to select in the interview
May u plz tell me how Vector uses thread safe methods while ArrayList uses non thread safe methods..ur rest info is very good..thanks in advance..
Hi,
Superb blog articles – read every one of them !
One request – can you start one more subsection – Collections? I did find abt read only collections and diff bw Vector/AL, but will appreciate few more, as it is really pretty easy to grasp the topics as you pen them….
Keep up the good work. One of the very few blogs that I like :)
We need Some More Examples about collection..
its very useful for me…still i need some example….thank u
better to add some code snippets that will understand very easily.
its very useful for me…though i need some example….
thank u
Hi its very nice explaination..Still can elaborate using sample coding for synchronised and unsynchronized…..
Hi Joe,
an excellent blog to explain it. arranged it very well.
Very nice explaination
Hi nice expalnation …
could please help me ,by letting me know how to do the following ……….
i have class A with two variable one is string and another is int , know i create an object of this class and add it to a arraylist ….. till here it fine but i am not able to print it ……
/* something like this
class A
{
String name=”hello”;
int num=100;
}
class TestArrayList
{
P S V m(S[] a)
{
A objA=new A();
Arraylist al=new Arraylist();
al.add(objA);
// how to print this arraylist ?? :-)
}
}
*/
Thanks in advance ……
A obj = new A();
ArrayList list = new ArrayList();
list.add(obj);
Iterator iterator = list.iterator();
while(iterator.hasNext()){
A a = (A)iterator.next();
System.out.println(a.name);
System.out.println(a.num);
}
Hi,
could u please explain me which one is better to use in JSP pages, whether to use vector or arrayList in JSP Application ??
Thanks in advance
thanks sir very good example
Thanks a lot for giving such knowledge
Sangeeta sahu
Thanks a lot for sharing precious information about vector ,array and array list.. its really been worth reading.
its good……..
Very helpful..
Thanks for giving an insight about the differences between Vector and ArrayList. As per the JavaDoc for ArrayList says below:
The details of the growth policy are not
specified beyond the fact that adding an element has constant amortized time cost.
So THE GROWTH POLICY OF AN ArrayList CANNOT BE SPECIFIED.
Its Great!!!!
your explanation is very good is sir and it is easily understandable to everybody but i want to know how can we know that a vector is synchronised. array list is not synchronised
Hi Joe
My question is
“if ArrayList is there,Why we go for vector in collection”.U think due to synchronization.u can do it as ArrayList by SynchronizedList()as thread safe”.
Which one is used in real time project in IT
industry.
i have a point that, vector can grow and can shrink according to the elements present in it. but Arraylist cant, we have to explicitly invoke the method trimtosize() to shrink it.vector is mostly used in web applications.
Please let me know the default size of the arraylist, vector and hash table
plz give me examples of arraylist,vector in realtime
Finally i got the answer thank u sir…….
-Vilas R.Dandge
Hi Joe,
Its very good blob, i got a lot of info in this. Would you explain one more difference?
List list = new ArrayList();
ArrayList list = new ArrayList();
what is the difference between above two?
THANK U
It would be great if you can put the comparison in a table.
Hi Joe It’s good, easily can understand the things.
put it in a Table
Its easy to understand.
Really Helpful.
Thank You.
Its very nice definition for beginners like me now i clearly understand the difference thank you.
niceone
Very nice explanation.. Explanation with an working example would be simply awesome and help in understanding the concepts behind them.
Thanks for this.
Excellent………
Good to understand about vector and arraylist
Now in Java 1.5, instead of using Vector for Synchronized, we can use CopyOnWriteArrayList which is way better than using Vector in certain cases. Please explain the difference between them as its a hot question in interview these days.
Is there any practical example to prove the statement:-By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.
Why vector and array list? why applet and swing? why hashmap and hastable? why ejb and spring? why string and string buffer? why errors and exceptions?
good explanation….but we need example to understand as practically
Anybody Or Joe
Please explain internal Array structure how collection are built on top of it
*Initial Capacity of ArrayList and Vector is 10
*Only difference is that ArrayList doesn’t have the capacity() Method while Vector has.
Internal implementation is similar for both the classes to set the initial size to 10.
@Joe
Challenging question to you ….
what could be the reason with design prospective , capacity method has not put inside the ArrayList ?
its good way of presenting about vector and arraylist
plse give with programming
thanks
Beautifully explained….very nice….Thanks Joe
Hi joe
Tell me what particular situvation arraylist are not used vectors only used
what is collection object
hey very nice concepts
how to convert 2-dimensional array to hashmap
how to convert 2-dimensional array to hashmap
We can make arraylist as thread safe then why Vector is still available in Collection?
Why Java does not deprecate this API?
how the ArrayList can be synchronized?
please send the answer to my mailid
Very good keep it up….
hi,
your explanation good……..thanks for spending your valuable time…….
Good explanation.But Can U provide if you have any programmatic experience about vector. Only this is the situation only vector is the solution like that way.
Thanks
Just what I needed.
Great. Keep it up.
Thanks
Good Article indeed!
good
Hi..
Good Explanation.
I have small doubt..
we can covert the Array list as Synchronization easily..
and why we go for vector…??
if it is require to synchronization, we can use Array list Synchronization formula..
so why vector is required….
Pl.share me exact ans…
Hi joe ,
pls can u share some diffferences between vectors and hash table !!!
hi joe,
i am bit confused with Load factor of Arraylist and Vector.
can u please share some details regarding Load factor??
Hi joe,
please tell me that how to reverse string in java without using function ?
e.q
I am indian.
output
indian am I.
Coming to load factor details:
Vector: Increases by 2 times if capacityIncrement is not specified,
hence newSize = size * 2;
else newSize = size + capacityIncrement;
ArrayList:
Where as ArrayList directly increments the size by 1.5 times!
Hence: newSize = ((size * 3)/2) +1;
But My question is
AbstractList implements List,
Vector extends AbstractList and implements List
Why Vector implements List again!!??
Anyone knows !?!?
@ram009.java@gmail.com
Why Vector is used still !!????
Answer is very simple ….
Now a days nobody prefers to use vector anymore … but still in JDK for lower versions compatibility only!!!
thank you. very helful
thank you, very informative
very use full tips………..
I really like your theme!
Great work… more than enough in the difference….
good blog…
Its a good work joe…
If its possible please tell me the way by which i can make an arraylist synchronized….
We can use Collections.synchronizedList(List arrayList) method to get the Synchronized list.
thnxs buddy for your answar………..
This is wrong.
Default size of arrayList and Vector is zero
u run javapapers what does it mean fucker
Good one. Keep it up.
Hi Joe, you have covered most of the points wellknown..i need some new points apart from the below points:
1.Vector is deprecated , ArrayList is not
2.Vector is Synchronized , ArrayList is not
3.Vector doubles its size , ArrayList is half when max size reaches
4.Vector is preferable, because it is threadsafe(allows one by one)
5. We can Synchronize Arraylist as Collections.synchronizedList(listObj);
6.Interms of performance Vector is better
apart from the above points Can you explain on both, how these will work in compiler level.
Thank you
Muralidhar N.
[…] So while programming, this behaviour should not be banked upon. Example for fail fast iterators are ArrayList, Vector, […]
Hi Upendra,
Map interface does not extends Iterable interface.So we can not use iterator() method to access the elements of Map classes. However, we can get the collection-view of a map by using entrySet() method .
Thanks,
Rajnish.
@shailendra you just go through the ArrayList class file. There u can that the default size of ArrayList and Vector is 10
load factor is applicable only to hashed collections
hi promod,
if you need storage collection is synchronized u can use vector other wise arralist.you can use synchronize arraylist in the case you want lost with synchronize
Synchronization
If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
Only one thread can call methods on a Vector at a time, and there’s a slight overhead in acquiring the lock; if you use an ArrayList, this isn’t the case.
default value of vector and arraylist is 10.
How collections are used?
please reply on my email address
its urgent.
reply me on bhavesh71291@gmail.com
thankyou
Load Factor of arrarList is 1, Vector List is 1.5
Hi Joe,
I have gone through many books and online tutorials but unable to find the correct answer.. finally I have decided to post you as your explanations on java have been so friendly and nice… So here is my doubts:
1) How to determine the capacity of an ArrayList? Which method is responsible for determining this in ArrayList? Does the LinkedList class capacity same as that of ArrayList or vector?
2) Can you please define Fill ratio (Load capacity) in friendly way…not able to get good definition..):
3) What is the Initial Capacity of HashSet, LinkedHashSet , TreeSet, HashMap, HashTable and TreeMap?
I know my demand is too much…but I need to know and expecting a quick reply :)
Hi,
As per as I know Collections is a class which contains many methods most common methods are sort…to sort the elements within collection and follow quick sort mechanism. Another one is syncronizedList, syncronizedSet and syncronizedMap methods to provide Syncronization version to the collection classes.
Hi Sandhya,
Both works in the same way however 1st approach is good. In this 1st approach we create a single reference variable of type List which refer to ArrayList object in the constant pool. Suppose we require to create LinkedList object in the same program so we can do like below:
list = new LinkedList(). So here single reference variable list points to two objects.
In the 2nd approach we create multiple reference variable pointing respective objects…..not a good approach though..
Thanks
Here is the code:
public class ReverseString
{
PSVM(String as [])
{
String Original=”I am Indian”;
String Reverse=””;
int length= Original.length();
for(int i=length-1; i>=0;i–)
{
Reverse=Reverse+charAt(i);
SOP(Reverse);
}
}
}
ArrayList can perform operation using Iterator only whereas vector can perform operation using Iterator as well as Enumeration.
I have been reading all the java posts of yours..Its amazing feeling reading them. You post them with such an ease they become very easy to read and understand. i am looking for more posts and updates from you. If you have any other blogs for other technologies kindly email me. Thanks and great work….!!!
Since jdk version 1.7.40, there is no initial capacity for the ArrayList and only the empty ArrayList will be created. You may see the documentation of arraylist with size 10 is created, but when you check its source there is only the emply list is being created and java team did not update that in their document.