2004/11/26 | Vector or ArrayList -- which is better?
类别(我的文档[原创]) | 评论(0) | 阅读(143) | 发表于 18:25
尝试一下翻译^_^
 Vector是有信号量的,可以同步锁,ArrayList没有,所以相对来说ArrayList的存取速度比Vector快,但是两者都有从新分配空间等额外的耗费。具体:
Find out the difference between Vector and ArrayList
By Tony Sintes
From JavaWorld
www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html
Vector or ArrayList -- which is better and why?

Sometimes Vector is better; sometimes ArrayList is better; sometimes you don't want to use either. I hope you weren't looking for an easy answer because the answer depends upon what you are doing. There are four factors to consider:


API
Synchronization
Data growth
Usage patterns
Let's explore each in turn.

API
In The Java Programming Language (Addison-Wesley, June 2000) Ken Arnold, James Gosling, and David Holmes describe the Vector as an analog to the ArrayList. So, from an API perspective, the two classes are very similar. However, there are still some major differences between the two classes.

Synchronization
Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList. Why pay the price of synchronization unnecessarily?

Data growth
Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element 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. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.

Usage patterns
Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. These operations are more expensive because you have to shift all elements at index i and higher over by one element. So what does this all mean?

It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1). However, indexing an element is a bit slower -- O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created.

Finally, in "PRAXIS 41" from Practical Java (Addison-Wesley, Feb. 2000) Peter Haggar suggests that you use a plain old array in place of either Vector or ArrayList -- especially for performance-critical code. By using an array you can avoid synchronization, extra method calls, and suboptimal resizing. You just pay the cost of extra development time.



问题:Vector还是Arraylist 哪个更好?
A:有时Vector好一点,有时ArrayList好一点,而有时候你都用不着。我相信你不可能找到更简单的答案,因为答案就在你的实践中。这有四个因素需要考虑的:
  API
  Synchronization
  Data growth
  Usage patterns
让我们依次来研究。
API
  在The Java Programming Language (Addison-Wesley, June 2000) Ken Arnold, James Gosling, and David Holmes 把Vector描述成ArrayList的类似物。所以,从一个API的观点看,这两个类是非常相似的。但是,它们之间仍然有一些大的区别。
Synchronization同步
  Vectors 是同步的。任何涉及vector内容的方法是线程安全的。Arraylist,从另一个方面是不同步的,比较当然是非线程安全啦。留意这个区别,使用同步会导致一个执行中断。所以如果你不需要线程安全的集合就使用ArrayList。为什么要为不必要的同步付出更多代价?
Date growth 数据增长
  在内部,Arraylist和vector都提供使用一个Array。当你在程序里使用它们的时候你需要把这个记在心里。当你往ArrayList或者Vector里添加一个元素,如果元素的数目超过给定内部数组(Array)时就需要展开它的内部数组(Array)。Vector默认按照它数组(Array)双倍增长,而ArrayList则是按它数组的50%增长。当你添加新的元素时,你可以终止发生大的中断,这取决于你如何使用这些类。如果你的程序需要,最好是把该对象的初始容量设置成最大。谨慎地设置容量,你以后就可以避免从新设置内部数组(Array)大小带来的耗费。如果你不知道你有多少数据但是你知道它的增长速率的话,Vector就有点优势-就是你可以设置它的增加值。
Usage patterns使用模式
  ArrayList和Vector都可以方便地从容器中的一个指定位置获取或者从容器的末尾添加和删除多个元素。它们所耗费的时间是一样的 -- O(1)。尽管如此,从其它位置添加和删除证明耗费是客观的,所花费的时间会呈线形增长 --O(n-i)(n是元素的个数,i是元素的索引)。这些操作开销更大,因为你得把在i和i以后的元素一个一个的转移。这意味着什么?
  这意味着如果你进行索引元素或者在Array后面添加和删除操作,使用vector或者arraylist都可以。如果你要进行其它的操作,就找其它的容器类。例如,LinkedList在任何位置添加或删除一个元素时间是一样的-- O(1)。但是索引一个元素会慢一点-- O(i) i是该元素的索引。使用ArrayList也更简单,因为你可以简单的使用一个索引而不是非得生成一个Iterator。LinkedList也是为每个元素创建一个内部对象。所以你必须清楚它也会带来了额外的耗费。







0

评论Comments