What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
Java – What are the differences between ArrayList and Vector
arraylistjavavector
Related Topic
- Java – How to read / convert an InputStream into a String in Java
- Java – What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do
- Java – the difference between JDK and JRE
- Java – Why is processing a sorted array faster than processing an unsorted array
- Java – difference between CrudRepository and JpaRepository interfaces in Spring Data JPA
- Java – What are the -Xms and -Xmx parameters when starting JVM
- Java – Why is printing “B” dramatically slower than printing “#”
- Java – Differences between Oracle JDK and OpenJDK
Best Answer
Differences
Use ArrayLists if there is no specific requirement to use Vectors.
Synchronization
If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
Collections.synchronizedList
is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.Data growth
Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.