8

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,554
  • 7
  • 36
  • 73
  • 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

14

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, the new implementations like ArrayList didn't have 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,982
  • 5
  • 33
  • 52
  • 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
  • Having used JDK 1.1 and following versions, the Collections Framework has always been a part of the standard JDK download, since 1.2, never a separate library. Although, and this from memory, the Collections Framework became a "Framework" with the release of 1.2, but Vector and a handful of other data structures have been there since before that. Framework meaning that the Sun developers, Josh Bloch, et al, thought the collection type classes had really gained notable maturity and cohesiveness. – dan Mar 10 '23 at 14:08
  • `java.util.Vector` has **ALWAYS** been synchronized, they never removed the synchronization from it. Your information is wrong. Just go look in the code, its still synchronized in JDK 1.11. It's the only `List` that is inherently synchronized. – dan Mar 10 '23 at 14:15
  • Dan, I was using collections library before it became part of jdk 1.2. At that time it was just another library. I believe packages were different as well. Not sure what sparked your second comment. I never said anything about synchronization removal from Vector. – ATrubka Mar 13 '23 at 14:36
  • @ATrubka I came on the Java scene with 1.1 and we upgraded a year later to 1.2 when it was released. We weren't using Collections before 1.2 bundled it. Indeed Vector was a part of 1.1. Perhaps you're referencing something else here: _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._ Reads like you're saying they removed the synchronization from Vector. Vector was moved into the Collections framework with 1.2, so your statement isn't clear if you mean something else. – dan Apr 26 '23 at 13:52
  • @dan, you're confusing Vector with various types of Lists. Vector predates Collections. I believe it was was part of Java from day one, but I don't really know day one. Vector was and still is synchronized. When they added collections, they changed Vector to implement List interface, but the synchronization stayed unchanged for backward compatibility. At that time people were discouraged from using older Vector and Hashtable and were suggested to use other List and Map implementations. Other implementations are typically not synchronized as it's not needed for most usages. – ATrubka Apr 27 '23 at 15:50
  • @ATrubka You haven't told me anything I didn't already know. The point is, your answer isn't clear - it appears we both agree on and lived through the facts. Your answer reads as tho you're saying they removed the synchronization from Vector - which is wrong and why I downvoted your answer and created my comment. There are Java developers who have not lived through this and aren't aware of these facts. – dan Apr 27 '23 at 19:09
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,726
  • 8
  • 51
  • 88
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
  • 133,369
  • 30
  • 199
  • 275