【攻克java集合系列(一)】java集合中的List系列集合全面分析

x33g5p2x  于2021-12-06 转载在 Java  
字(17.4k)|赞(0)|评价(0)|浏览(316)

java中的集合是一个庞大的体系,从现在开始我们一起来揭开java集合的神秘面纱。

今天先从List系列集合出发,然后逐步攻克java中庞大的集合体系。

List系列集合中的继承实现关系

建议大家先看完本文再回过头来思考这张图。

List集合的有序性与可重复性

List常称为列表,它是java集合大家庭中一个分支。list集合位于java.util.List包中,它是一个有序的集合,可以包含重复的元素,提供了按索引访问的方式。

List<Integer> list = new ArrayList<>();
list.add(9);
list.add(1);
list.add(6);
list.add(8);
list.add(6);
list.add(8);
list.add(4);

System.out.println(list);
System.out.println(list.get(0));
System.out.println(list.get(3));

可以看到,向List集合中插入元素的时候,按照先插的位置在前,后插的位置在后的原则,并且重复的元素并不会被覆盖,这就是List集合的有序性与可重复性。List集合也提供了get(int index)方法,可以通过索引index直接获取对应位置的元素。

List接口源码分析

List是java提供的一个接口,位于java.util.List包中,它继承至Collection接口。
在List接口中声明了许多基本方法:

继承于Collection中的方法

Collection是集合层次结构中的根接口,它提供了更具体的子接口的实现,如List 。 该接口通常用于传递集合,并在需要最大的通用性的情况下对其进行操作。

public interface Collection<E> extends Iterable<E> {
    // 返回此集合中的元素数。 如果超过Integer.MAX_VALUE个元素,则返回Integer.MAX_VALUE 。 
    int size();

    // 如果此集合不包含元素,则返回 true 。
    boolean isEmpty();

    // 如果此集合包含指定的元素,则返回true 。
    boolean contains(Object o);

    // 返回此集合中的元素的迭代器。
    Iterator<E> iterator();

    // 返回一个包含此集合中所有元素的数组。
    Object[] toArray();

    // 返回包含此集合中所有元素的数组; 返回的数组的运行时类型是指定数组的运行时类型。
    <T> T[] toArray(T[] a);

    // 如果此集合由于调用而更改,则返回true 。 (如果此集合不允许重复,并且已包含指定的元素,则返回false。 )
    boolean add(E e);

    // 从该集合中删除指定元素的单个实例(如果存在)(可选操作)。如果此集合包含指定的元素(或等效地,如果此集合由于调用而更改),则返回true 。 
    boolean remove(Object o);

    // 如果此集合包含指定 集合中的所有元素,则返回true。 
    boolean containsAll(Collection<?> c);

    // 将指定集合中的所有元素添加到此集合
    boolean addAll(Collection<? extends E> c);

    // 删除指定集合中包含的所有此集合的元素。 此调用返回后,此集合将不包含与指定集合相同的元素。 
    boolean removeAll(Collection<?> c);

    // 删除满足条件的此集合的所有元素。
    default boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }

    // 仅保留此集合中包含在指定集合中的元素,即从该集合中删除所有不包含在指定集合中的元素。
    boolean retainAll(Collection<?> c);

    // 清空集合
    void clear();

    // 将指定的对象与此集合进行比较
    boolean equals(Object o);

    // 返回此集合的哈希码值。
    int hashCode();

    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }

    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }

    default Stream<E> parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }
}

List中新声明的方法

public interface List<E> extends Collection<E> {
    // 将该列表的每个元素替换为将该运算符应用于该元素的结果。
    default void replaceAll(UnaryOperator<E> operator) {
        Objects.requireNonNull(operator);
        final ListIterator<E> li = this.listIterator();
        while (li.hasNext()) {
            li.set(operator.apply(li.next()));
        }
    }

    // 排序
    @SuppressWarnings({"unchecked", "rawtypes"})
    default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }

    // 返回此列表中指定位置的元素。
    E get(int index);

    // 用指定的元素替换此列表中指定位置的元素。 
    E set(int index, E element);

    // 将指定的元素插入此列表中的指定位置。
    void add(int index, E element);

    // 删除该列表中指定位置的元素。
    E remove(int index);

    // 返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1。
    int indexOf(Object o);

    // 返回此列表中指定元素的最后一次出现的索引,如果此列表不包含元素,则返回-1。
    int lastIndexOf(Object o);

    // 返回列表中的列表迭代器
    ListIterator<E> listIterator();

    // 从列表中的指定位置开始,返回列表中的元素的列表迭代器。
    ListIterator<E> listIterator(int index);

    //返回列表中指定的fromIndex (含)和toIndex之间的部分
    List<E> subList(int fromIndex, int toIndex);

    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, Spliterator.ORDERED);
    }
}

ArrayList、LinkedList和Vector

ArrayList、LinkedList、Vector都是List接口的实现类。

ArrayList

从源码分析ArrayList集合
ArrayList是集合框架里常用的数据结构。

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

ArrayLis继承至AbstractList,并且实现了List、RandomAccess、Cloneable以及Serializable接口,所以它支持快速访问、可复制以及序列化等操作。

在ArrayList的底层是使用数组来进行存储的,并且数组的容量可以动态的改变,初始容量为10。

/** * Default initial capacity. */
private static final int DEFAULT_CAPACITY = 10;

当容量不足的时候,ArrayList会自动扩容。新容量newCapacity = oldCapacity + (oldCapacity >> 1)

/** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        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);
    }

构造函数
ArrayList源码中设置了三种ArrayList的构造方式:

  • ArrayList():构造一个初始容量为十的空列表。
public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
  • ArrayList(int initialCapacity):构造具有指定初始容量的空列表。
public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }
  • ArrayList(Collection<? extends E> c):构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序。
public ArrayList(Collection<? extends E> c) {
        Object[] a = c.toArray();
        if ((size = a.length) != 0) {
            if (c.getClass() == ArrayList.class) {
                elementData = a;
            } else {
                elementData = Arrays.copyOf(a, size, Object[].class);
            }
        } else {
            // replace with empty array.
            elementData = EMPTY_ELEMENTDATA;
        }
    }

常用方法

  • int size()
    返回此列表中的元素数。
public int size() {
        return size;
    }
  • boolean isEmpty()
    如果此列表不包含元素,则返回 true 。
public boolean isEmpty() {
        return size == 0;
    }
  • boolean contains(Object o)
    如果此列表包含指定的元素,则返回true 。
public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }
  • int indexOf(Object o)
    返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1。
public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
  • int lastIndexOf(Object o)
    返回此列表中指定元素的最后一次出现的索引,如果此列表不包含元素,则返回-1。
public int lastIndexOf(Object o) {
        if (o == null) {
            for (int i = size-1; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = size-1; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
  • Object clone()
    返回此ArrayList实例的浅拷贝。 (元素本身不被复制)
public Object clone() {
        try {
            ArrayList<?> v = (ArrayList<?>) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }
  • Object[] toArray()
    以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。
public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }
  • E get(int index)
    获取指定索引的元素
public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }
  • E set(int index, E element)
    用指定的元素替换此列表中指定位置的元素,返回原来的元素。
public E set(int index, E element) {
        rangeCheck(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }
  • 向列表中添加元素(add())
    1.将指定的元素追加到此列表的末尾。
public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
  1. 在此列表中的指定位置插入指定的元素。 将当前位于该位置的元素(如果有)和任何后续元素(向其索引添加一个)移动。
public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }
  1. 按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾。
public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }
  1. 将指定集合中的所有元素插入到此列表中,从指定的位置开始。
public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);

        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }
  • void clear()
    清空列表中的元素
public void clear() {
        modCount++;

        // clear to let GC do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }
  • 删除列表中的元素(remove())
  1. 删除该列表中指定位置的元素。 将任何后续元素移动到左侧(从其索引中减去一个元素)。
public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }
  1. 从列表中删除指定元素的第一个出现(如果存在)。 如果列表不包含该元素,则它不会更改。
public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }
  1. 此列表中删除所有索引为fromIndex (含)和toIndex之间的元素。 将任何后续元素移动到左侧(减少其索引)。
protected void removeRange(int fromIndex, int toIndex) {
        modCount++;
        int numMoved = size - toIndex;
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        // clear to let GC do its work
        int newSize = size - (toIndex-fromIndex);
        for (int i = newSize; i < size; i++) {
            elementData[i] = null;
        }
        size = newSize;
    }
  1. 从此列表中删除指定集合中包含的所有元素。
public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, false);
    }
  1. 从此列表中删除其中不包含在指定集合中的所有元素。
public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, true);
    }
  1. 批量删除
private boolean batchRemove(Collection<?> c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            for (; r < size; r++)
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {
            // Preserve behavioral compatibility with AbstractCollection,
            // even if c.contains() throws.
            if (r != size) {
                System.arraycopy(elementData, r,
                                 elementData, w,
                                 size - r);
                w += size - r;
            }
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                modCount += size - w;
                size = w;
                modified = true;
            }
        }
        return modified;
    }
  • List subList(int fromIndex, int toIndex)
    返回指定的fromIndex (含)和toIndex之间的列表部分
public List<E> subList(int fromIndex, int toIndex) {
        subListRangeCheck(fromIndex, toIndex, size);
        return new SubList(this, 0, fromIndex, toIndex);
    }
  • void replaceAll(UnaryOperator operator)
    将该列表的每个元素替换为将该运算符应用于该元素的结果。
public void replaceAll(UnaryOperator<E> operator) {
        Objects.requireNonNull(operator);
        final int expectedModCount = modCount;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            elementData[i] = operator.apply((E) elementData[i]);
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }
  • void sort(Comparator<? super E> c)
    排序
public void sort(Comparator<? super E> c) {
        final int expectedModCount = modCount;
        Arrays.sort((E[]) elementData, 0, size, c);
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

LinkedList

从源码分析LinkedList集合
LinkedList就是把我们常说的链表结构,也是List中常用的一种集合。

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

LinkedList继承至AbstractSequentialList(AbstractList的子类),并且实现了List、Deque、Cloneable、Serializable接口,所以它也是可复制、可序列化的。

LinkedList底层使用双向链表进行数据的维护。初始长度为0,first和last指针分别指向首尾结点。

transient int size = 0;

    /** * Pointer to first node. * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */
    transient Node<E> first;

    /** * Pointer to last node. * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */
    transient Node<E> last;

其中Node结点的数据结构(双向链表结构)定义如下:

private static class Node<E> {
        E item;		//元素值
        Node<E> next;		//指向前一个结点
        Node<E> prev;		//指向后面一个结点

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

构造方法
LinkedList提供了两种构造方法:

  • LinkedList():构造一个空列表
public LinkedList() {
    }
  • LinkedList(Collection<? extends E> c):构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序。
public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

常用方法

  • int size()
    返回此列表中的元素数。
public int size() {
        return size;
    }
  • E getFirst()
    返回列表中的第一个元素。
public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }
  • E getLast()
    返回列表中的最后一个元素。
public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }
  • E removeFirst()
    从列表中的删除并返回第一个元素。
public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }
    private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // help GC
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }
  • E removeLast()
    从列表中的删除并返回最后一个元素。
public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }
    private E unlinkLast(Node<E> l) {
        // assert l == last && l != null;
        final E element = l.item;
        final Node<E> prev = l.prev;
        l.item = null;
        l.prev = null; // help GC
        last = prev;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }
  • void addFirst(E e)
    在列表的开头插入指定元素。
public void addFirst(E e) {
        linkFirst(e);
    }
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }
  • void addLast(E e)
    在列表的末尾插入指定元素。
public void addLast(E e) {
        linkLast(e);
    }
    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }
  • boolean contains(Object o)
    如果列表中包含指定元素,返回true。
public boolean contains(Object o) {
        return indexOf(o) != -1;
    }
  • int indexOf(Object o)
    返回列表中指定元素第一次出现的索引,如果不存在该元素则返回-1。
public int indexOf(Object o) {
        int index = 0;
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }
  • int lastIndexOf(Object o)
    返回列表中指定元素最后一次出现的索引,如果不存在该元素则返回-1。
public int lastIndexOf(Object o) {
        int index = size;
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (x.item == null)
                    return index;
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (o.equals(x.item))
                    return index;
            }
        }
        return -1;
    }
  • 向列表中添加元素(add())
    1.将指定的元素追加到此列表的末尾。
public boolean add(E e) {
        linkLast(e);
        return true;
    }
  1. 在此列表中的指定位置插入指定的元素。
public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }
  1. 按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾。
public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }
  1. 将指定集合中的所有元素插入到此列表中,从指定的位置开始。
public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0)
            return false;

        Node<E> pred, succ;
        if (index == size) {
            succ = null;
            pred = last;
        } else {
            succ = node(index);
            pred = succ.prev;
        }

        for (Object o : a) {
            @SuppressWarnings("unchecked") E e = (E) o;
            Node<E> newNode = new Node<>(pred, e, null);
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        if (succ == null) {
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;
        modCount++;
        return true;
    }
  • void clear()
    清空列表中的元素
public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        // more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }
  • boolean remove(Object o)
    从列表中删除指定元素的第一个出现(如果存在)。 如果列表不包含该元素,则它不会更改。
public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }
  • Object clone()
    返回此ArrayList实例的浅拷贝。 (元素本身不被复制)
public Object clone() {
        LinkedList<E> clone = superClone();

        // Put clone into "virgin" state
        clone.first = clone.last = null;
        clone.size = 0;
        clone.modCount = 0;

        // Initialize clone with our elements
        for (Node<E> x = first; x != null; x = x.next)
            clone.add(x.item);

        return clone;
    }
  • Object[] toArray()
    以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。
public Object[] toArray() {
        Object[] result = new Object[size];
        int i = 0;
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;
        return result;
    }
  • E get(int index)
    获取指定索引的元素
public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }
  • E set(int index, E element)
    用指定的元素替换此列表中指定位置的元素,返回原来的元素。
public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

队列相关的方法
LinkedList实现了Deque接口,所以LinkedList也包含了一些队列相关的方法。

  • E peek()
    检索但不删除此列表的头。
public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }
  • E element()
    检索但不删除此列表的头。
public E element() {
        return getFirst();
    }
  • E poll()
    检索并删除此列表的头。
public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }
  • E remove()
    检索并删除此列表的头。
public E remove() {
        return removeFirst();
    }
  • boolean offer(E e)
    将指定的元素添加为此列表的尾部
public boolean offer(E e) {
        return add(e);
    }
  • boolean offerFirst(E e)
    在列表的头部插入指定元素
public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }
  • boolean offerLast(E e)
    在列表的尾部插入指定元素。
public boolean offerLast(E e) {
        addLast(e);
        return true;
    }
  • E peekFirst()
    检索但不删除列表的第一个元素,如果为空,返回null。
public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }
  • E peekLast()
    检索但不删除列表的最后一个元素,如果为空,返回null。
public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }
  • E pollFirst()
    检索并删除列表的第一个元素,如果为空,返回null。
public E pollFirst() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }
  • E pollLast()
    检索并删除列表的最后一个元素,如果为空,返回null。
public E pollLast() {
        final Node<E> l = last;
        return (l == null) ? null : unlinkLast(l);
    }
  • void push(E e)
    列表的头部插入指定元素。
public void push(E e) {
        addFirst(e);
    }
  • E pop()
    删除并返回列表的第一个元素
public E pop() {
        return removeFirst();
    }

Vector

Vector通常称之为向量,它与ArrayList较为相似,底层也是使用数组存储数据,并动态的扩充数组的容量。但是Vector中的每个方法都添加了synchronized关键字修饰,某一时刻只有一个线程能够写Vector,它是线程安全的,但效率比较低下。

public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable

与ArrayList一样,Vector继承至AbstractList,并且实现了List、RandomAccess、Cloneable以及Serializable接口,所有它支持快速访问、可复制以及序列化等操作。

ArrayList、LinkedList、Vector三者的比较

相关文章