English 中文(简体)
java.util.Vector - alternatives
原标题:

Previously I would always have thought a Vector was good to use for non-descript objects when length was unknown. As far as I was aware I thought it was thread-safe too

What would change that Vector shouldn t be used anymore, and what is the alternative?

最佳回答

You should use ArrayList instead of Vector. Vector used internal synchronisation, but that is rarely good enough for actual consistency, and only slows down execution when it is not really needed.

Also see this stackoverflow question.

问题回答

You can use an ArrayList instead.

If you need a synchronized version, you can do something like:

ArrayList arrayList = new ArrayList();

List synchList = Collections.synchronizedList(arrayList);

ArrayList is now the better class to use. Vector is now considered Legacy, and has the added performance overhead of being Thread-Safe.

Use ArrayList when you need a List implementation but don t need thread safety, and use CopyOnWriteArrayList when you need a List implementation that is thread safe.

Vector is a legacy collection class from Java 1.0. In Java 1.2 (long ago!), the Collections Framework was added which included new collection classes such as ArrayList and HashMap, which were intended to replace the legacy classes Vector and Hashtable.

As said before, the legacy collection classes had built-in synchronization, which is unnecessary for many applications. Synchronization has a performance overhead, so if it s not necessary, you shouldn t use it.

In some cases (when your program is multi-threaded, and multiple threads access the same data) you need to synchronize your collections. Some people would then use the old Vector or Hashtable classes, but a better way is to use a synchronization wrapper with for example an ArrayList:

// Your standard, unsynchronized list
List<String> data = new ArrayList<String>();

// Use this to put it into a synchronization wrapper
List<String> syncedData = Collections.synchronizedList(data);

See the API documentation of Collections.synchronizedList() (and other methods) for more information.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签