Analysis and comparison of java Vector and ArrayList

  • 2020-05-19 04:49:14
  • OfStack

java Vector and ArrayList

Today, I studied the source code of Vector and ArrayList and deepened my understanding of these two classes.

List interface 1 implements three classes: ArrayList, Vector, LinkedList. LinkedList doesn't say much about it. It is used mostly to keep the insertion order of data.

Both ArrayList and Vector are implemented using arrays, with three main differences:

1. Vector is multithreaded security, while ArrayList is not. It can be seen from the source code that many methods in Vector class are modified by synchronized, which leads to the fact that Vector cannot be compared with ArrayList in efficiency.
2, both are used in linear continuous space storage elements, but when the space is insufficient, the increase of the two kind of way is different, many netizens said Vector increases 1 times that of the original space, ArrayList increase 50% of the original space, in fact the same mean, but there's one little problem can see from the source, 1 for analysis from the source.
3. Vector can set the growth factor, while ArrayList cannot. When I first read this, I did not understand what is the increment factor, but I understood it by comparing the two source codes.

ArrayList has three constructors: one is


public ArrayList(int initialCapacity)// structure 1 An empty list with the specified initial capacity.  
public ArrayList()// structure 1 The initial capacity is zero 10 Empty list.  
public ArrayList(Collection<? extends E> c)// structure 1 Individual inclusion specification  collection  The list of elements  

Vector has four constructors:


public Vector()// Constructs with the specified initial capacity and a capacity increment equal to zero 1 It's a null vector.  
public Vector(int initialCapacity)// structure 1 An empty vector that makes the size of its internal data array, its standard capacity increment to zero.  
public Vector(Collection<? extends E> c)// structure 1 Individual inclusion specification  collection  The vectors of the elements of  
public Vector(int initialCapacity,int capacityIncrement)// Constructs with the specified initial capacity and capacity increment 1 Some empty vector  

Vector has one more construction method than Arraylist, which is public Vector(int initialCapacity,int capacityIncrement). capacityIncrement is the capacity growth, which is the growth factor mentioned above, which is not found in ArrayList.

Then post two additional classes under the source code analysis (jdk1.7 version) :


//ArrayList Class add source code:  
  public boolean add(E e) { 
    ensureCapacityInternal(size + 1); // Increments modCount!! 
    elementData[size++] = e; 
    return true; 
  } 
  private void ensureCapacityInternal(int minCapacity) { 
    modCount++; 
    // overflow-conscious code 
    // If you add 1 After 10 elements, the size of the new container is larger than the capacity of the container, so it cannot store values and needs to expand the space  
    if (minCapacity - elementData.length > 0) 
      grow(minCapacity); 
  } 
  private void grow(int minCapacity) { 
    // overflow-conscious code 
    int oldCapacity = elementData.length; 
    int newCapacity = oldCapacity + (oldCapacity >> 1); // The expanded space adds to the original 50% That is to say, the original 1.5 Times)  
    if (newCapacity - minCapacity < 0) // If the container is not expanded enough, it will simply be expanded minCapacity I'm going to make it the size of the container  
      newCapacity = minCapacity; 
    if (newCapacity - MAX_ARRAY_SIZE > 0) // If the expanded container is too large, execute hugeCapacity 
      newCapacity = hugeCapacity(minCapacity); 
    // minCapacity is usually close to size, so this is a win: 
    elementData = Arrays.copyOf(elementData, newCapacity); 
  } 

Vector class add source code:


  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 - elementData.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 requires a judgment call: if the capacity increment is not initialized 0 , even in use public Vector(int initialCapacity,int capacityIncrement) The initialization performed by the constructor, then the capacity expansion is (oldCapacity+capacityIncrement) , is the original capacity plus the value of the capacity increment; If the capacity increment is not set, then the capacity after expansion is (oldCapacity+oldCapacity) That's the original capacity 2 Times.  
    **/ 
    if (newCapacity - minCapacity < 0) 
      newCapacity = minCapacity; 
    if (newCapacity - MAX_ARRAY_SIZE > 0) 
      newCapacity = hugeCapacity(minCapacity); 
    elementData = Arrays.copyOf(elementData, newCapacity); 
  } 

Through the analysis, it should be understandable now!

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


Related articles: