[ACCEPTED]-java.util.Vector - alternatives-arraylist
You should use ArrayList
instead of Vector
. Vector
used internal 4 synchronisation, but that is rarely good 3 enough for actual consistency, and only 2 slows down execution when it is not really 1 needed.
Also see this stackoverflow question.
You can use an ArrayList
instead.
If you need a synchronized 1 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 2 considered Legacy, and has the added performance 1 overhead of being Thread-Safe.
Use ArrayList
when you need a List
implementation but 3 don't need thread safety, and use CopyOnWriteArrayList
when 2 you need a List
implementation that is thread 1 safe.
Vector
is a legacy collection class from Java 16 1.0. In Java 1.2 (long ago!), the Collections Framework was added 15 which included new collection classes such 14 as ArrayList
and HashMap
, which were intended to replace 13 the legacy classes Vector
and Hashtable
.
As said before, the 12 legacy collection classes had built-in synchronization, which 11 is unnecessary for many applications. Synchronization 10 has a performance overhead, so if it's not 9 necessary, you shouldn't use it.
In some 8 cases (when your program is multi-threaded, and 7 multiple threads access the same data) you 6 need to synchronize your collections. Some 5 people would then use the old Vector
or Hashtable
classes, but 4 a better way is to use a synchronization 3 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 2 documentation of Collections.synchronizedList()
(and other methods) for 1 more information.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.