Hi,
I have another question.
What is the advantage of arraylist over vector.
Why arraylist is widely used instead of vector in java programming?
Thanks in advance.....

Dani AI

Generated

Short, interview-ready take: ArrayList is the go-to list implementation for new code; Vector exists mainly for backward compatibility and for a different concurrency model. Mentioning that and a quick note about modern concurrency options usually answers an interviewer's intent.

A few concrete points worth adding to the replies above:

  • Capacity growth: ArrayList implementations typically grow by about 50% when resized; Vector historically doubles its capacity unless you supply a capacityIncrement. See the official docs for details (ArrayList, Vector).
  • Iteration and legacy API: Vector has older API baggage (Enumeration, addElement/elementAt). ArrayList uses the Collections/Iterator family and the usual fail-fast behavior; when you need safe concurrent iteration consider the concurrent utilities instead of relying on legacy classes.
  • Practical concurrency choices: use Collections.synchronizedList(...) when you need a simple synchronized wrapper, or pick a class from java.util.concurrent such as CopyOnWriteArrayList for read-heavy scenarios. The concurrent collection docs discuss trade-offs (CopyOnWriteArrayList, Collections.synchronizedList).

Example snippets to keep in your interview toolkit:

List<String> sync = Collections.synchronizedList(new ArrayList<>());
synchronized(sync) {
    for (String s : sync) { /* safe iteration */ }
}
List<String> cow = new CopyOnWriteArrayList<>();
// safe to iterate without extra synchronization (good for many reads, few writes)

Acknowledging and : thread concerns are valid, but name the specific alternatives and why they’re better than using Vector in modern code.

Recommended Answers

All 6 Replies

don't use Vector unless you have to and remember that you don't have to unless you've no other option (like it's a required argument to some method). That's all you need to know.

may be it is a legacy class. but I was asked both the questions in Interview.
That is why I need to know the answers.

Well in relation to threads, I major difference is that a Vector is synchronized and an ArrayList is not synchronized. What this means is, if two threads attempt to access a
Vector it will be synchronized. One thread will have to wait until the
other thread is finished accessing the Vector. With an ArrayList you have
to create wrappers to synchronize access to the ArrayList or you will have
undefined behaviour.

Also:

may be it is a legacy class. but I was asked both the questions in Interview.
That is why I need to know the answers.

If you were asked that in a job interview and didn't know the answer they're right to fail you.
If you applied for a job where you should know the answer and you not only don't know it but don't know where to find it (and it's easy to find, everyone should know who's programmed in Java for more than a day or so) you don't deserve any job at all.

Nice Answer Jwenting,

You are obviously an asshole and don't deserve any friends.

In answer to the original question. The main reason is that Vector is internally synchronized and so if you aren't using threads then you have an unnecessary performance hit. Also, internal synchronization is not very good for managing data concurrently (not very thread-safe) so even when it should be used you shouldn't use Vector but rather an ArrayList in conjunction with a concurrency class.

Hope this helps. Good luck in those interviews.

If you were asked that in a job interview and didn't know the answer they're right to fail you.
If you applied for a job where you should know the answer and you not only don't know it but don't know where to find it (and it's easy to find, everyone should know who's programmed in Java for more than a day or so) you don't deserve any job at all.

commented: troll -2

Jwenting said probably said that because googling "vector arraylist java" returns thousands of helpful results to the exact question the OP asked. Why reiterate a question that has been answered so many times that can be found so easily? And you upping this thread was also unnecessary. Look at the date it was posted.

For example, this article, although outdated, provides enough information to answer the OP's question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.