What are the differences between ArrayList and Vec

2020-01-23 04:43发布

What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?

8条回答
▲ chillily
2楼-- · 2020-01-23 04:58

Basically both ArrayList and Vector both uses internal Object Array.

ArrayList: The ArrayList class extends AbstractList and implements the List interface and RandomAccess (marker interface). ArrayList supports dynamic arrays that can grow as needed. It gives us first iteration over elements. ArrayList uses internal Object Array; they are created with an default initial size of 10. When this size is exceeded, the collection is automatically increases to half of the default size that is 15.

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. Vector is having four constructors out of that one takes two parameters Vector(int initialCapacity, int capacityIncrement) capacityIncrement is the amount by which the capacity is increased when the vector overflows, so it have more control over the load factor.

Some other differences are: enter image description here

查看更多
SAY GOODBYE
3楼-- · 2020-01-23 05:00

As the documentation says, a Vector and an ArrayList are almost equivalent. The difference is that access to a Vector is synchronized, whereas access to an ArrayList is not. What this means is that only one thread can call methods on a Vector at a time, and there's a slight overhead in acquiring the lock; if you use an ArrayList, this isn't the case. Generally, you'll want to use an ArrayList; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a Vector.

查看更多
我想做一个坏孩纸
4楼-- · 2020-01-23 05:03

ArrayList is newer and 20-30% faster.

If you don't need something explitly apparent in Vector, use ArrayList

查看更多
时光不老,我们不散
5楼-- · 2020-01-23 05:07

Differences

  • Vectors are synchronized, ArrayLists are not.
  • Data Growth Methods

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.

Reference

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.

Reference

查看更多
我想做一个坏孩纸
6楼-- · 2020-01-23 05:09

Vector came along with the first version of java development kit (JDK). ArrayList was introduced in java version 1.2, as part of java collections framework. ArrayList and Vector both use Array as a data structure internally. Both classes keep the insertion order. They both allow duplicate and null values. However there are few differences in the way they store and process data.

Synchronization

  • ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time.
  • Vector is synchronized, which means only one thread at a time can access the code.

Performance

  • ArrayList gives better performance as it is non-synchronized .
  • Vector operations gives poor performance as they are thread-safe.

Traversal

  • ArrayList uses the Iterator interface to traverse the elements.
  • Vector can use both Iterator interface and Enumeration interface to traverse the elements.

Data Growth

ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage – but the way they resize is different.

  • ArrayList increments 50% of the current array size if the number of elements exceeds its capacity.
  • Vector increments 100% – essentially doubling the current array size.

Legacy

Earlier versions of Java did not include the Collections Framework. The original classes were re-engineered to support the collection interface. These classes are known as Legacy classes.

  • ArrayList is not a legacy class. It is introduced in JDK 1.2.
  • Vector is a legacy class.

Set Increment Size

  • ArrayList does not define the increment size .
  • Vector defines the increment size .

If there is a need to perform “thread-safe” operation the Vector is your best bet as it ensures that only one thread access the collection at a time. But if there is no need for thread safe operation, ArrayList is a better choice as performance will be improved because of the concurrent processes.

查看更多
Anthone
7楼-- · 2020-01-23 05:16

ArrayList and Vector both implements List interface and maintains insertion order.But there are many differences between ArrayList and Vector classes...

ArrayList -

  1. ArrayList is not synchronized.
  2. ArrayList increments 50% of current array size if number of element exceeds from its capacity.
  3. ArrayList is not a legacy class, it is introduced in JDK 1.2.
  4. ArrayList is fast because it is non-synchronized.
  5. ArrayList uses Iterator interface to traverse the elements.

Vector -

  1. Vector is synchronized.
  2. Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.
  3. Vector is a legacy class.

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

  5. Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.

See Also : https://www.javatpoint.com/difference-between-arraylist-and-vector

查看更多
登录 后发表回答