Analysis and comparison of Java vector and ArrayList _java

Source: Internet
Author: User

Java Vector and ArrayList comparisons

Today we studied vector and ArrayList source code, and deepened the understanding of these two classes.

A total of three classes are implemented under the list interface: Arraylist,vector,linkedlist. LinkedList is not much to say, it is generally used to keep the data in the insertion order.

ArrayList and vectors are all made of arrays, with three major differences:

1, Vector is multi-threaded security, and ArrayList is not, this can be seen from the source code, the vector in many of the methods have synchronized to modify, which led to the vector in efficiency can not be compared with ArrayList;
2, two are the use of linear continuous space storage elements, but when the space is insufficient, two classes to increase the way is different, many netizens said vector increase the original space of one times, ArrayList increase the original space of 50%, in fact, is almost the meaning of this, But a little bit of the problem can be seen from the source code, a moment from the source analysis.
3, vector can set the growth factor, and ArrayList can not, the first time to look at this, I do not understand what is the increment factor, but by comparing the two source to understand this, first look at two classes of the construction method:

The ArrayList has three construction methods:

The public ArrayList (int initialcapacity)//Constructs an empty list with the specified initial capacity. Public 
ArrayList ()//constructs an empty list with an initial capacity of 10. Public 
ArrayList (collection<? extends e> c)//Constructs a list of elements that contain the specified Collection 

Vector has four construction methods:

The public vector ()//uses the specified initial capacity and the capacity increment equal to zero to construct an empty vector. The public 
vector (int initialcapacity)//is constructed with an empty vector so that its internal data array is sized with a standard capacity increment of zero. 
the public vector (collection<. extends e> c)//constructs a vector public vector that contains the elements in the specified Collection 
(int initialcapacity , int capacityincrement)//constructs an empty vector using the specified initial capacity and capacity increment 

Vector is one more method of constructing than ArrayList, and yes is the method of constructing the public Vector (int initialcapacity,int capacityincrement), Capacityincrement is capacity growth, the growth factor mentioned above, which is not in ArrayList.

Add source analysis of two classes again (jdk1.7 version):

Add source for the ArrayList class: public boolean Add (e) {ensurecapacityinternal (size + 1);//increments modcount!! 
    elementdata[size++] = e; 
  return true; 
    } private void ensurecapacityinternal (int mincapacity) {modcount++; Overflow-conscious Code//If the new container is larger than the capacity of the container after adding an element, then the value cannot be stored, and the space needs to be expanded if (Mincapacity-elementdata.length > 
  0) grow (mincapacity); 
    } private void Grow (int mincapacity) {//overflow-conscious code int oldcapacity = elementdata.length; int newcapacity = oldcapacity + (oldcapacity >> 1); Expanded space increases the original 50% (that is, 1.5 times times the original) if (Newcapacity-mincapacity < 0)//If the container is not enough after the expansion, then simply set the mincapacity to the size of the container NEWC 
    apacity = mincapacity; 
    if (newcapacity-max_array_size > 0)//If the expanded container is too large, then execute hugecapacity newcapacity = hugecapacity (mincapacity); Mincapacity is usually close to size, so this is a win:elementdata = arrays.copyof (Elementdata, newcapacity) 
  ; 
 }

Add source of vector class:   

  Public synchronized Boolean Add (E e) {modcount++; 
    Ensurecapacityhelper (Elementcount + 1); 
    elementdata[elementcount++] = e; 
  return true; } private void Ensurecapacityhelper (int mincapacity) {//Overflow-conscious code if (MINCAPACITY-ELEMENTDA 
  Ta.length > 0) grow (mincapacity); 
    } private void Grow (int mincapacity) {//overflow-conscious code int oldcapacity = elementdata.length; 
                     int newcapacity = oldcapacity + (capacityincrement > 0)? 
    capacityincrement:oldcapacity); /** This expansion needs to be judged: if capacity increment initialization is not 0, that is, the use of the public Vector (int initialcapacity,int capacityincrement) Construction method of initialization, then the capacity of expansion is ( 
    Oldcapacity+capacityincrement), is the original capacity plus capacity increment of the value, if not set capacity increment, then the capacity after the expansion is (oldcapacity+oldcapacity), is the original capacity of twice times. 
    **/if (newcapacity-mincapacity < 0) newcapacity = mincapacity; 
    if (newcapacity-max_array_size > 0) newcapacity = hugecapacity (mincapacity); Elementdata = ArrayS.copyof (Elementdata, newcapacity); 
 }

Through the analysis, now should be able to understand it!

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.