Java Vector and ArrayList resolve differences

Vector inherited AbstractList, implements List, RandomAccess, Cloneable, java.io.Serializable, and consistent ArrayList

Vector substantially coincides with the ArrayList, but the following are a few differences

1 Initialization
Default constructor with no arguments will Vector initializes an array of length 10, ArrayList and then create an array in the specific call.
In comparison, the delay of loading ArrayList less space
2 expansion (Grow ())
the Vector When increment is 0, expanded to twice its original size, when delta is greater than 0, expanded to the original size incremented ArrayList extensions algorithm: half the original size of the array plus the original array
Why Vector chosen wasteful way of expansion space, where I believe the principles of C ++ Vector and Java Vector should be consistent, https://www.zhihu.com/question/36538542/answer / 67,929,747
. 3 security thread
Vector is thread safe, ArrayList not thread safe
thread safe class Vector includes inside thereof the iterators implement class ListItr

In fact, the biggest difference is that thread safety, of course, if we want to create a thread-safe ArrayList, you can call Collections.synchronizedList (), get a static inner classes SynchronizedList, synchronous code block processing ArrayList.
This thread-safe way to get the ArrayList and Vector What is the difference? Very simple, one difference between the synchronized block and synchronization method, and the rest is in addition to other differences ArrayList and the Vector security thread; Another point can not be ignored, synchronous implementation iterator former requires the user to manually control

 public ListIterator<E> listIterator() {
            return list.listIterator(); // Must be manually synched by user
        }

This is SynchronizedList iterator acquisition

But Collections.synchronizedList () into parameters of type List, other implementation class can be passed as LinkedList List

Guess you like

Origin www.cnblogs.com/elinlinlinlog/p/11402997.html