Newsletter

Difference between Arraylist and Vector in Java

Core Java » on Nov 2, 2012 { 10 Comments } By Sivateja

Hi friends, let us see the main differences between ArrayList and Vector collections, this is very important and common question in interviews for all IT people, hope you might face this question in your previous interviews ๐Ÿ˜‰

Any how let us see the main differences and which is better regarding the performance

  • ArrayList and Vector both having same data structure internally, which is Array
  • Vector is by default synchronized, means at a time only one thread can access its methods from out side, where as ArrayList is non-synchronized means N number of threads can access at a time
  • But we can make ArrayList as synchronized by using Collections class, see it bellow
  • Both Vector and ArrayListย  have capability to re-size dynamically, means Vector will Doubles the size of its array when its size increased, but ArrayList increased by Half only ๐Ÿ™‚

How to make ArrayList Synchronized

As we discussed above ArrayList is not synchronized by default, so will see how to make it as Synchronized.

We have direct method in Collections class to make ArrayList synchronized..

List li = new ArrayList()
Collections.synchronizedList(li)

ArrayList vs Vector Speed and Performance Differences

Always ArrayList will shows better performance compared to Vector,ย  except Synchronization both are almost same in their performance.ย  Vector is Synchronized means thread safe, only 1 thread can access so its very slow compared to ArrayList, because in our real time projects we should not require synchronized methods always.

What am saying is always try to use ArrayList rather Vector if its not required any Synchronization in your requirements,ย  even if so you know how to make ArrayList as synchronized right (just like above).

 

โ€‹ โ€‹โ€‹

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

10 Responses to “Difference between Arraylist and Vector in Java”
  1. Naresh says:

    If we synchronize arraylist,which is very poor in performance right?please explain me why?

  2. If We Can Synchornize ArrayList , then what is the use of Vector? which Senario we use the vector then?

  3. vasanth says:

    what is the Advantage of default initial capacity in ArrayList?

  4. Surya says:

    What are the initial sizes for both ArrayList & Vector ?

  5. Rajkumar says:

    what is the default capacity in vector ?

  6. kumar says:

    How to generate the Barcode in jsp. please send me sample application using to eclipse?

  7. ravi says:

    Hi ,

    please code for spring mvc with hibernate for CRUD operations

  8. Saurabh Ranjan says:

    Can we make arraylist as synchronized?If yes,what is the use of Vector ?

  9. honey says:

    when should i go for vector?

  10. Anurag Singh says:

    Hello sir
    this is really very helpful and nice article about arraylist and vector in java.
    thank you so much for this post
    please keep posting for us.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.