java collection class - list

Posted by eheia on Tue, 01 Feb 2022 07:33:25 +0100

1. ArrayList

public class ArrayList<E> extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
}
  • RandomAccess # is a flag interface, which indicates that the List collection implementing this interface supports fast random access. In ArrayList, we can quickly obtain the element object through the element serial number, which is fast random access.
  • ArrayList , implements the , clonable , interface, which overrides the function clone(), and can be cloned.
  • ArrayList , implements , Java io. Serializable interface, which means that ArrayList supports serialization and can be transmitted through serialization.

Key points:

1. Default initial capacity

private static final int DEFAULT_CAPACITY = 10;

The current size of the ArrayList instance is modified. Applications can use this operation to minimize the storage of ArrayList instances.

​public void trimToSize() {
        ++this.modCount;
        if (this.size < this.elementData.length) {
            this.elementData = this.size == 0 ? EMPTY_ELEMENTDATA : Arrays.copyOf(this.elementData, this.size);
        }
    }

​

3. Expand the capacity to 1.5 times of the original capacity each time

    /**
     * ArrayList The core method of capacity expansion.
     */
    private void grow(int minCapacity) {
        // oldCapacity is the old capacity and newCapacity is the new capacity
        int oldCapacity = elementData.length;
        //Move oldCapacity one bit to the right, which is equivalent to oldCapacity /2,
        //We know that bit operation is much faster than division operation. The result of whole sentence operation is to update the new capacity to 1.5 times of the old capacity,
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        //Then check whether the new capacity is greater than the minimum required capacity. If it is still less than the minimum required capacity, take the minimum required capacity as the new capacity of the array,
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        //Then check whether the new capacity exceeds the maximum capacity defined by ArrayList,
        //If it exceeds, call hugeCapacity() to compare minCapacity with MAX_ARRAY_SIZE,
        //If minCapacity is greater than MAX_ARRAY_SIZE, then the new capacity is interger MAX_ Value, otherwise, the new capacity size is MAX_ARRAY_SIZE. 
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

 

 

 

 

1.1. What is the difference between ArrayList and Vector?

  • ArrayList , is the main implementation class of , List , and the bottom layer uses , Object [] storage. It is suitable for frequent search work, and the thread is unsafe;
  • Vector , is an ancient implementation class of , List , which is stored in , Object [] at the bottom and thread safe.

1.2. What is the difference between ArrayList and LinkedList?

  1. Whether thread safety is guaranteed: ArrayList and LinkedList are not synchronized, that is, thread safety is not guaranteed;
  2. Underlying data structure: Arraylist , the underlying uses the , Object , array; The LinkedList # bottom layer uses the # two-way linked list # data structure (before JDK1.6, it was a circular linked list, and JDK1.7 canceled the cycle.)
  3. Whether insertion and deletion are affected by element location: ① ArrayList is stored in array, so the time complexity of inserting and deleting elements is affected by element location. For example, when the add (E) method is executed, ArrayList # will append the specified element to the end of the list by default. In this case, the time complexity is O(1). However, if you want to insert and delete elements at the specified position I, the time complexity of (add(int index, E element)) is O(n-i). Because when performing the above operations, the (n-i) elements after the I and I elements in the set must perform the operation of moving backward / forward by one bit. ② LinkedList is stored in a linked list, so for the insertion of add (E, e) method, the time complexity of deleting elements is not affected by the element position, which is approximately O(1). If you want to insert and delete elements at the specified position I ((add(int index, E element)) time complexity is approximately o(n)), because you need to move to the specified position before inserting.
  4. Whether it supports fast random access: LinkedList does not support efficient random element access, while ArrayList does. Fast random access is to quickly obtain the element object (corresponding to the get(int index) method) through the element serial number.
  5. Memory space occupation: the space waste of ArrayList is mainly reflected in that a certain capacity space is reserved at the end of the list, while the space cost of LinkedList is reflected in that each element of ArrayList needs to consume more space than ArrayList (because it needs to store direct successors, direct precursors and data).

 

 

Topics: data structure