Compare ArrayList Java and basic knowledge of LinkedList

A basic concept:
First look at these two types of implements List interface, and List interfaces to achieve a total of three categories, namely ArrayList, Vector and LinkedList. List for storing a plurality of elements, it is possible to maintain order of the elements, and the elements allowing repeated. Related concrete implementation class distinction 3 as follows:

(1) Memory
ArrayList and Vector: is the order of the storage element, deleting an element, after the operation is completed, the elements necessary to shift part, 10 is the default initial capacity.

ArrayList and Vector-based arrays are implemented, LinkedList implementation is based on a doubly linked list (containing the head node).

(2) security thread
ArrayList **** does not have any thread safety , in a single-threaded environment, the LinkedList is thread safe , if they are used in a concurrent environment, may be C ollections Class Static Method synchronizedList () for ArrayList and LinkedList can be called.
The API explanation:

synchronizedList
public static <T> List<T> synchronizedList(List<T> list)返回指定列表支持的同步(线程安全的)列表。为了保证按顺序访问,必须通过返回的列表完成所有对底层实现列表的访问。
在返回的列表上进行迭代时,用户必须手工在返回的列表上进行同步: 

  List list = Collections.synchronizedList(new ArrayList());
      ...
  synchronized(list) {
      Iterator i = list.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }
 不遵从此建议将导致无法确定的行为。 
如果指定列表是可序列化的,则返回的列表也将是可序列化的。 
参数:
list - 被“包装”在同步列表中的列表。 
返回:
指定列表的同步视图。

Vector achieve thread-safe , most of the methods that it contains the keyword synchronized, but the efficiency is not ArraykList Vector and LinkedList high.

(3) the expansion mechanism
from the interior in terms of implementation mechanism, the ArrayList and Object Vector is used in the form of an array to store , when adding elements to both types of time, if the capacity is not enough, the need for expansion. ArrayList capacity after expansion before the 1.5 times , and then copy the data to the new array before go. And Vector default capacity after expansion of the case 2 before the fold .

Vector incremental capacity may be provided, but not ArrayList . In Vector, there capacityIncrement: when the size of its large capacity, the capacity is automatically increased amount. If when creating Vector, specifies the size of capacityIncrement, Vector the need to increase capacity in a dynamic array, if the increase in volume is greater than 0, the increase in the size capacityIncrement, if the increment is less than 0, the increase of the previous 2 times.

It should say something in a variable-length array of principle: When the number of elements exceeds the length of the array, will produce a new array, copy the original data array into a new array, and then add new elements to the new array.

(4) efficiency CRUD
ArrayList and Vector, the retrieval of an object from a specified location, or the end of the collection insert, delete an element of time is the same, the time complexity is O (1). But if you add or remove elements of time spent elsewhere is O (n), LinkedList, the insert, delete any position of time it takes the elements are the same, all the time complexity is O (1), but he in a time complexity of retrieval element is O (n).

So if just to find a specific location of the element or move elements only increase at the end of the set, then use an ArrayList or Vector is the same. If it is inserted in the specified location, delete elements, the best choice LinkedList

II summarizes differences:
(1) about the difference between ArrayList and Vector follows:
①: ArrayList default low memory expansion is 50% + 1, Vector is the default extension 1 fold.
②: Vector provides indexOf (obj, start) interfaces, ArrayList no.
③: Vector belong thread safety level, but do not use Vector most cases, because of the need for greater security thread overhead.

(2) the difference between ArrayList and LinkedList as follows:
1. For ArrayList and LinkedList terms, add an element to the end of the list are spent on fixed costs. For ArrayList, the main increase in an internal array, pointing to the added elements, may occasionally lead to re-allocate the array; while LinkedList is concerned, this overhead is a unified, assign an internal Entry object.
2. Inserting or deleting an element in the middle of the ArrayList means that the remaining elements in the list will be moved; and inserting or deleting an element in the middle of LinkedList cost is fixed.
3. LinkedList does not support efficient random access elements.
4. Waste of space ArrayList is mainly reflected in the list at the end of the list to reserve a certain volume of space and the space it takes LinkedList is reflected in every element needs to consume considerable space

When the operation is to add the data behind a column of data, rather than in the front or in the middle and requires random access to its elements when using ArrayList will provide better performance; when your operations are added in front or middle of a data or when you delete data, and access to its elements in the order, you should use a LinkedList.

So, if only to find elements of a particular location or only at the end of the set increases, remove elements, then use the Vector or ArrayList can be. If the insertion of other specified location, delete operation, the best choice LinkedList.

Published 99 original articles · won praise 2 · Views 2612

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105251433