preface

Today we are going to learn about Vector and Stack, two “outdated” containers. Although they are not likely to be used in actual development, it is necessary to learn about them for the integrity of List containers. Before we learn, we need to remember the data structure of the Stack.

A brief introduction to Vector and Stack

Vector is a thread-safe ArrayList based on synchronized implementation, but the capacity expansion mechanism is slightly different from ArrayList when inserting elements, and the capacity expansion can be controlled by passing capacityIncrement. Stack is implemented based on Vector and supports LIFO.

Vector extends from AbstractList, RandomAccess, Serializable, Cloneable, etc. Stack extends directly from Vector.

Analyze Vector and Stack from source code

Member variables

protected Object[] elementData;// The array is actually stored
protected int elementCount;// The number of elements in the container
protected int capacityIncrement;// The amount increased by each capacity expansion. If this parameter is not specified, the capacity will be doubled each time
Copy the code

A constructor

public Vector(int initialCapacity) {// Specify the initial size
        this(initialCapacity, 0);
    }
public Vector(a) {// If not specified, the default initial size is 10
        this(10);
    }
public Vector(int initialCapacity, int capacityIncrement) {// Specify the initial size and the increment. The increment is how much the container capacity increases with each expansion
    super(a);if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];
    this.capacityIncrement = capacityIncrement;
}
public Vector(Collection<? extends E> c) {
    elementData = c.toArray();
    elementCount = elementData.length;
    // c.toArray might (incorrectly) not return Object[] (see 6260652)
    if(elementData.getClass() ! = Object[].class) elementData = Arrays.copyOf(elementData, elementCount, Object[].class); }Copy the code

The Stack constructor is an empty parameter that calls the parent’s empty parameter constructor, which defaults to size 10.

add

//
public synchronized boolean add(E e) {// Synchronized is used to ensure thread safety
    modCount++;
    ensureCapacityHelper(elementCount + 1);// Make sure the capacity is sufficient
    elementData[elementCount++] = e;// Add elements to the end of the array
    return true;
}
Copy the code

delete

public boolean remove(Object o) {
        return removeElement(o);
    }
public synchronized boolean removeElement(Object obj) {
    modCount++;
    int i = indexOf(obj);
    if (i >= 0) {
        removeElementAt(i);
        return true;
    }
    return false;
}
public synchronized void removeElementAt(int index) {
    modCount++;
    if (index >= elementCount) {
        throw new ArrayIndexOutOfBoundsException(index + "> =" +
                                                 elementCount);
    }
    else if (index < 0) {
        throw new ArrayIndexOutOfBoundsException(index);
    }
    int j = elementCount - index - 1;
    if (j > 0) {
        System.arraycopy(elementData, index + 1, elementData, index, j);
    }// Use arrayCopy to concatenate the following directly into the original array, overwriting the element to be deleted directly
    elementCount--;
    elementData[elementCount] = null; /* to let gc do its work */// Then release the last element
}
Copy the code

Update and query

Set and get are pretty simple and I won’t go into detail

E set(int index, E element);// Replace the elements at the specified position in the Vector with the specified element.
E get(int index);// Returns the element at the specified position in this vector.
Copy the code

Stack section

E peek(a);// View the object at the top of the stack without removing it from the stack.
E pop(a);// Remove the object at the top of this stack and return that object as the value of this function.
E push(E item);// Push the item to the top of the stack.
Copy the code

conclusion

Vector is thread-safe compared to the other two in the List, and the default size of Vector is double, with the default initial size being 10. Because it is thread-safe through synchronized, it is inefficient.

This concludes the List section, and the next section will look at the key maps of containers, including HashMap, HashTable, LinkedHashMap, and TreeMap.