An ArrayList is different from a Vector

The difference between a

The ArrayList and Vector classes are both implemented using arrays, but ArrayList uses the transient keyword when defining the elementData attribute

private transient Object[] elementData;  (ArrayList)

protected Object[] elementData;  (Vector)
Copy the code

transient

Java language keyword, variable modifier. If you declare an instance variable transient, its value does not need to be maintained while the object is stored. Object storage here refers to a mechanism provided by Java’s Serialization to persist object instances. When an object is serialized, transient variables are not included in the serialized representation, whereas non-transient variables are included. The usage case is that when an object is persisted, there may be a special object data member that we do not want to use serialization mechanisms to store. To turn off serialization on a field for a particular object, you can prefix the field with the keyword TRANSIENT. , is the transient modified member variable, its value will be ignored during serialization, after deserialization, the transient variable value is set to the initial value, such as int is 0, object is NULL.

The difference between the two

ArrayList implements the writeObject method, which stores data only at non-NULL array locations. ElementData for the size number of the list. An additional point to note is that the implementation of ArrayList, which provides a fast-fail mechanism, can provide weak consistency.

Fast – fail mechanism: hollischuang. Gitee. IO/tobetopjava…

Vector also implements writeObject, but the writeObject method is not optimized for storage like ArrayList. The clone() statement copies null values as well. So Vector and ArrayList, which hold the same content, take up more bytes than ArrayList

The difference between the three

ArrayList is a more efficient single-threaded data structure (compared to Vector) for asynchronous implementations. ArrayList provides weak consistency with only one change record field, mainly used in iterators. There is no synchronization method. The fast-fail mechanism mentioned above. The storage structure of ArrayList is defined as transient, and writeObject is overwritten to implement custom serialization and optimize storage. ,

Vector is a more reliable data structure for multithreaded environments, with all methods synchronized.

The difference between the four

Vector requests double its size at a time, whereas ArrayList increases its size by 50% at a time

This article is a learning summary of the Java engineer to become a god

Hollischuang. Gitee. IO/tobetopjava…