7

If multiple threads access vector, vector will ensure that only one thread can access the vector at the same time. SynchronizedList is same. So what's the difference? How to choose in some synchronous situation?

roast_soul
  • 3,474
  • 6
  • 35
  • 71
  • Consider using [`CopyOnWriteArrayList`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html) instead. – Eng.Fouad Feb 18 '13 at 08:32
  • It is ordinarily too costly. – Subhrajyoti Majumder Feb 18 '13 at 08:36
  • 1
    This question has been marked as a duplicate - but the question it supposedly duplicates hasn't been answered (to be more precise, it has one "answer", but it doesn't actually answer the question). – Dan King Jul 19 '16 at 09:14

3 Answers3

12

The main reason for this redundancy is backward compatibility with java code developed for old versions of Java.

If I remember correctly before Java 1.2 Collections were a separate library and were not part of standard JDK/JRE.

At that point the only list like data structure supplied by SDK was Vector. In Collections developers improved that Vector structure in many ways. Particularly, they removed synchronization as it proved to be unnecessary in most of the cases.

Still they wanted to allow an easy way to create a synchronized version of list like collection. Thus they introduced SynchronizedList.

The main difference now between Vector and SynchronizedList is the way you use it. By calling Collections.synchronizedList you create a wrapper around your current List implementation, which means you don't copy data to another data structure and you keep underlying structure intact. For instance, if you want LinkedList structure behind it as opposed to ArrayList.

In case of a Vector, you actually copy the data to the new list like structure Vector. So it's less efficient in case you had a list before, but if you didn't have any data structure before, then you may want to use Vector as it doesn't add method wrapping cost to every method invocation. Another disadvantage of a vector is that you can't keep alternative underlying structure (such as LinkedList), you always use what's been implemented in the Vector itself.

ATrubka
  • 3,902
  • 3
  • 31
  • 49
  • Reading the javadoc for Vector, I don't see anywhere where it says objects added to a vector are copied to a new list like structure, can you expand on / or double check your point in the last paragraph ? – AfterWorkGuinness May 17 '14 at 22:11
  • That's not exactly what I said. Elements of a list are not copied. My point was that if you were to create a synchronized Vector or List out of an existing not synchronized List you would have to copy data from one to another (something like list2.addAll(list1)). If you do so, the underlying data structure gets copied (not the elements though). I suggest you open Vector.java, AbstractList.java and other related classes to see how the data gets added from one collection to another. It's going to be much clearer for you then. – ATrubka Jun 02 '14 at 09:14
1

Java Vectors are syncronized by default. You don't have to synchronize is explicitly. Even if you do so there is no extra benefit

Use of Java Vector is highly discouraged and instead Syncronized "ArrayList" is suggested. There are clearly benefits of the same.

Also please refer Are Vectors Obsolete ?

Community
  • 1
  • 1
Vinod Jayachandran
  • 3,478
  • 6
  • 37
  • 76
0

Vector is a synchronized List implementation and Collections.synchronziedList is a method of Collections utility class that creates a synchronized proxy for any List implementation

Evgeniy Dorofeev
  • 129,181
  • 28
  • 195
  • 266