Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java vector to arraylist

Is there any way to copy or convert a vector to arraylist in Java?

like image 330
coder247 Avatar asked Aug 09 '10 12:08

coder247


People also ask

Which is better ArrayList or Vector?

Performance: ArrayList is faster. Since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe), if one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.

Why ArrayList is faster than Vector in Java?

4) ArrayList is fast because it is non-synchronized. Vector is slow because it is synchronized, i.e., in a multithreading environment, it holds the other threads in runnable or non-runnable state until current thread releases the lock of the object.

Is Vector same as ArrayList in Java?

Vector: Vector is similar to ArrayList but the differences are, it is synchronized and its default initial size is 10 and when the size exceeds its size increases to double of the original size that means the new size will be 20. Vector is the only class other than ArrayList to implement RandomAccess.


Video Answer


1 Answers

Yup - just use the constructor which takes a collection as its parameter:

Vector<String> vector = new Vector<String>(); // (... Populate vector here...) ArrayList<String> list = new ArrayList<String>(vector); 

Note that it only does a shallow copy.

like image 172
Jon Skeet Avatar answered Sep 20 '22 19:09

Jon Skeet