Difference between Vector and ArrayList

 

https://zhidao.baidu.com/question/501985714.html

 

Both of these two classes implement the List interface (the List interface inherits the Collection interface), and they are both ordered collections, that is, the positions of the elements stored in the two collections are ordered, which is equivalent to a dynamic array , we can take out an element according to the position index number in the future, and the data in it is allowed to be repeated. This is the biggest difference between sets such as HashSet. Sets such as HashSet cannot retrieve elements by index number. , and no duplicate elements are allowed (the original question has nothing to do with hashset, but in order to clarify the functions of ArrayList and Vector, we use a comparison method, which is more conducive to explaining the problem).

 

Then I will talk about the difference between ArrayList and Vector, which mainly includes two aspects:. (1) Synchronization:

       Vector is thread-safe, that is, its methods are thread-synchronized, while ArrayList is thread-unsafe, and its methods are thread-asynchronous. If only one thread will access the collection, it is best to use ArrayList, because it does not consider thread safety, and the efficiency will be higher; if multiple threads will access the collection, it is best to use Vector, because we do not need our own Then think about and write thread-safe code.

 

Remarks: For Vector&ArrayList, Hashtable&HashMap, keep in mind the issue of thread safety, remember that Vector and Hashtable are old, provided by java as soon as they were born, they are thread-safe, ArrayList and HashMap are provided only when java2, they is thread unsafe. So, let's talk about the old ones first. (2) Data growth:

       Both ArrayList and Vector have an initial capacity size. When the number of elements stored in them exceeds the capacity, the storage space of ArrayList and Vector needs to be increased. Every time the storage space needs to be increased, not only one storage unit is added. , but to increase multiple storage units, and the number of storage units added each time must achieve a certain balance between memory space utilization and program efficiency. Vector grows to twice the original size by default, while the growth strategy of ArrayList is not clearly specified in the document (from the source code, it is seen that the growth is 1.5 times the original size). Both ArrayList and Vector can set the initial space size, and Vector can also set the growth space size, while ArrayList does not provide a method to set the growth space.

 

    Summary: That is, Vector doubles the original size, and ArrayList increases 0.5 times the original size.

 

1. Vector & ArrayList 
1) The methods of Vector are all synchronized and thread-safe, while the methods of ArrayList are not. Because the synchronization of threads will inevitably affect the performance, the performance of ArrayList is better than that of Vector. Okay. 
2) When the elements in Vector or ArrayList exceed its initial size, Vector will double its capacity, while ArrayList will only increase its size by 50%, so ArrayList will help save memory space.

2. Hashtable & HashMap 
Hashtable and HashMap are similar in performance to Vector and ArrayList. For example, the method of Hashtable is synchronous, while that of HashMap is not.


3. ArrayList & LinkedList

ArrayList的内部实现是基于内部数组Object[],所以从概念上讲,它更象数组,但LinkedList的内部实现是基于一组连接的记录,所以,它更象一个链表结构,所以,它们在性能上有很大的差别: 
       从上面的分析可知,在ArrayList的前面或中间插入数据时,你必须将其后的所有数据相应的后移,这样必然要花费较多时间,所以,当你的操作是在一列数据的后面添加数据而不是在前面或中间,并且需要随机地访问其中的元素时,使用ArrayList会提供比较好的性能; 而访问链表中的某个元素时,就必须从链表的一端开始沿着连接方向一个一个元素地去查找,直到找到所需的元素为止,所以,当你的操作是在一列数据的前面或中间添加或删除数据,并且按照顺序访问其中的元素时,就应该使用LinkedList了。 
 

如果在编程中,1、2两种情形交替出现,这时,你可以考虑使用List这样的通用接口,而不用关心具体的实现,在具体的情形下,它的性能由具体的实现来保证。


4. 配置集合类的初始大小 
    在Java集合框架中的大部分类的大小是可以随着元素个数的增加而相应的增加的,我们似乎不用关心它的初始大小,但如果我们考虑类的性能问题时,就一定要考虑尽可能地设置好集合对象的初始大小,这将大大提高代码的性能。 
    比如,Hashtable缺省的初始大小为101,载入因子为0.75,即如果其中的元素个数超过75个,它就必须增加大小并重新组织元素,所以,如果你知道在创建一个新的Hashtable对象时就知道元素的确切数目如为110,那么,就应将其初始大小设为110/0.75=148,这样,就可以避免重新组织内存并增加大小。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326308594&siteId=291194637