What Are the Differences Between Arraylist and Vector

What are the differences between ArrayList and Vector?

Differences

  • Vectors are synchronized, ArrayLists
    are not.
  • Data Growth Methods

Use ArrayLists if there is no specific requirement to use Vectors.

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.

Collections.synchronizedList is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.

Data growth

Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

ArrayList vs Vector : Which one to use?

Other than differences with synchronization, there is also a difference internally when you insert new elements. For both classes, the array within must be increased in size to prevent it from running out of room. Vectors double their size by default. ArrayLists increase their size by half their present size.

Edit:

Also, a couple answers mentioned that Vectors are thread safe. That's true to some extent, but the whole reason they're sort-of-deprecated (I think) is because they're not very useful for most synchronization needs, which is because it synchronizes on every operation (not safe). What you want to do usually is synchronize on a sequence of operations. Vectors don't do that (neither do ArrayLists though) so it's not really the way to go even in situations that require synchronization (basically it's out of your control).

ArrayList vs. Vectors in Java if thread safety isn't a concern

Vector originates back from the pre-Collections API days, and have been retrofitted since to be a part of it. From what I've read, the reason it is not deprecated is because the core API depends on it.

ArrayList was written from scratch as a part of the Collections API and as such should be used unless you need to support Java versions down to 1.2.

If you need a thread-safe ArrayList, you can use the static factory method Collections.synchronizedList(new ArrayList<type>); to generate your list.

Collections: Array, Vector and ArrayList. Differences and appropriate usage

Some history:

  • Vector exists since Java 1.0;
  • the List interface exists since Java 1.2, and so does ArrayList;
  • Vector has been retrofitted to implement the List interface at that same time;
  • Java 5, introducing generics, has been introduced in 2004 (link).

Your course, dating back 2005, should have had knowledge of ArrayList at the very list (sorry, least), and should have introduced generics too.

As to Array, there is java.lang.reflect.Array, which helps with reflections over arrays (ie, int[], etc).

Basically:

  • Vector synchronizes all operations, which is a waste in 90+% of cases;
  • if you want concurrent collections, Java 5 has introduced ConcurrentHashMap, CopyOnWriteArrayList etc, you should use those;
  • DO NOT use Vector anymore in any event; some code in the JDK still uses it, but it is for backwards compatibility reasons. In new code, there are better alternatives, as mentioned in the previous point;
  • since Java 1.2, Vector does not offer the same thread safety guarantees as it used to offer anyway.

The latter point is interesting. Prior to Iterator there was Enumeration, and Enumeration did not offer the possibility to remove elements; Iterator, however, does.

So, let us take two threads t1 and t2, a Vector, and those two threads having an Iterator over that vector. Thread t1 does:

while (it.hasNext())
it.next();

Thread t2 does:

// remember: different iterator
if (!it.hasNext())
it.remove();

With some unlucky timing, you have:

t1                  t2
------ ------
hasNext(): true
.hasNext(): false
removes last element
.next() --> BOOM

Therefore, Vector is in fact not thread safe. And it is even less thread safe since Java 5's introduction of the "foreach loop", which creates a "hidden" iterator.

Vector vs. ArrayList which is better?

As per this question Vector is considered "obsolete", use ArrayList instead.



Related Topics



Leave a reply



Submit