Difference #IT star is not a dream ArrayList, LinkedList and Vector's

ArrayList, linkedList and Vector are inherited from the List interface. ArrayList and Vector underlayer is a dynamic array, the bottom 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 ( the 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 comparison consumption performance.

image.png

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

image.png

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

image.png

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.

image.png

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:

image.png

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

1.      Adding an 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 + 1size - index);

image.png

image.png

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

 

In the LinkedList overhead and insert elements in the collection final cost is about, just put it in front of an element pointer pointing to themselves, their pointer to the next element on it.

 

in conclusion:

1.      ArrayList and the Vector structure are the same as dynamic arrays, except that the ArrayList is not thread-safe, Vector is thread-safe, ArrayList performance is better than Vector, ArrayList is the largest collection of applications.

2. The      difference between ArrayList and LinkedList they are different data structures, ArrayList is a dynamic array, LinkedList is a doubly linked list, more in the search operation, with less insert at a particular location data, and delete data generally used in cases ArrayList, in a specific location insert data, delete data more operations, query the case of less is generally used LinkedList, but in most applications query efficiency requirements are high, so the ArrayList collection of some of the more widely used.


Guess you like

Origin blog.51cto.com/11583017/2470191