The difference between ArrayList, LinkedList and Vector's

ArrayList, LinkedList and Vector are inherited from the List interface. ArrayList and the Vector is the underlying dynamic array, the underlying LinkedList is a doubly linked list.

The difference is that ArrayList ArrayList and Vector are thread safe, Vector is thread-safe methods in the Vector are synchronized method (synchronized), so the efficiency is higher than Vector ArrayList, it is also the most widely used set.

We focus on the difference between ArrayList and LinkedList compare the difference between ArrayList and LinkedList is actually the difference between arrays and doubly linked lists.

It features an array: Because the array is allocated a contiguous memory space, use the index to find elements is very fast. However, insert, delete data performance is relatively low.

Teresa should delete the elements you need to first start from Wang Zhiwen moved forward one by one, to Teresa overwritten.

Then look at the structure of two-way linked list:

In each element in the list is stored in addition to the content itself is also an element of a pointer and a pointer to the storage element before the point, the figure shows a doubly linked list contains three elements, each linked list has a header portion, a head pointer to the first element, the rear element also points to the head.

Doubly linked list of features, low query efficiency, because the query a query elements need to start from the head, one by one through each element until you find the required element, insert, delete high efficiency instance, we delete it directly to an element of the previous element a pointer to an element after it will be able to:

We look at the source code to parity characteristics of the two sets of data structures

1. Add element to the tail of the collection: List interface provides a method to add elements to the end of the set of boolean add (E e), ArrayList and LinkedList implement this method:

Look at how the ArrayList is implemented:

public boolean add(E e) {
    ensureCapacityInternal(size + 1); //判断当前数组的容量是否够大如果不够大则扩容
    elementData[size++] = e;//将元素添加到数组尾部
    return true;
}

 

这里的执行效率取决于:ensureCapacityInternal(size+ 1)方法的执行,在该方法中会判断数组容量是否足够,如果不够则进行扩容到原来的1.5倍。在扩容的过程中会生成一个新的数组,将原数组中的元素复制到新数组中。所以在这种情况下如果数组容量足够大ArrayList的效率是非常高的,我们也可以根据实际情况给它一个合适的初始值。

再来看一下LinkedList集合:

public boolean add(E e) {
    linkLast(e);
    return true;
}
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++;
}    

 

我们可以看到每新增一个元素就要创建一个Node对象,进行频繁的赋值操作 “final Node<E> newNode = new Node<>(l, e, null);”对效率有一定的影响。


我们再来看一下在特定的位置插入元素:

List接口中提供的方式是这样的:void add(int index,E element)

在ArrayList中是这样实现的:

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++;
}

 

从代码中我们可以看出,每插入一个元素就要进行大量元素复制操作:“System.arraycopy(elementData, index, elementData, index + 1,size - index);”

从插入点往后的元素依次后移将新元素插入到空出来的位置上。效率非常低下。

而在LinkedList中开销和在集合最后插入元素开销差不多,只需要把它前一个元素的指针指向自己,自己的指针指向下一个元素就可以了。

总结一下:

1. ArrayList和Vector结构一样都是动态数组,区别是ArrayList是线程不安全的,Vector是线程安全的,ArrayList性能好于Vector,ArrayList是应用最多的集合。

2. ArrayList和LinkedList的区别是他们的数据结构不同,ArrayList是动态数组,LinkedList是双向链表,在查询操作较多,在特定位置插入数据和删除数据较少的情况下一般选用ArrayList,在特定位置插入数据,删除数据操作较多,查询较少的情况下一般选用LinkedList,但是在大多数应用中都是对查询效率要求较高,所以ArrayList集合应用更广泛一些。

Guess you like

Origin www.cnblogs.com/lbky/p/12116487.html