What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Vector
is a broken class that is not threadsafe, despite it being "synchronized" and is only used by students and other inexperienced programmers.ArrayList
is the go-to List implementation used by professionals and experienced programmers.Professionals wanting a threadsafe List implementation use a
CopyOnWriteArrayList
.There are 2 major differentiation's between Vector and ArrayList.
Vector is synchronized by default, and ArrayList is not. Note : you can make ArrayList also synchronized by passing arraylist object to Collections.synchronizedList() method. Synchronized means : it can be used with multiple threads with out any side effect.
ArrayLists grow by 50% of the previous size when space is not sufficient for new element, where as Vector will grow by 100% of the previous size when there is no space for new incoming element.
Other than this, there are some practical differences between them, in terms of programming effort:
When to use which one?
Note : even though arraylist grows by 100%, you can avoid this by ensurecapacity() method to make sure that you are allocating sufficient memory at the initial stages itself.
Hope it helps.