I. Overview
List inherits Collection, is an ordered list.
Data may be repeated
Implementation class has ArrayList, LinkedList, Vector, Stack, etc.
-
- ArrayList is implemented based on an array, the array is a queue. You can dynamically add capacity!
- LinkedList is based on the realization of the list, is a two-way circulation list. It can be used as stack use!
- Vector -based arrays to achieve, is a vector queue is thread-safe!
- Stack is based on the array to achieve, is the stack, it inherits the Vector, is characteristic of FILO (last out)
Second, the use of scenarios
1. When the collection rate of the data elements inserted less demanding, but requires fast access to data elements, using the ArrayList!
2. When set to speed data access elements do not ask for much, but for high speed insertion and deletion of data elements required, the use LinkedList!
3. When the set of multithreaded collection element operating time, use the Vector! But now BVector now generally no longer used. For use in a multi-threaded and can use CopyOnWriteArrayList, in the java.util.concurrent package.
4. When there is a demand set is to save the data first read out, using Stack!
Third, each Profile
3.1 ArrayList
List ArrayList is the most common implementation class, the interior is through an array of implementation, it allows elements fast random access . Disadvantage is that the array can not be spaced between each element of the array size is not satisfied when the storage capacity needs to be increased, it is necessary to have copied to the new array of data storage space. When inserting or deleting an element from the intermediate position of ArrayList, the array is required to copy, move, the cost is relatively high. Therefore, it is suitable for random search and traversal, not suitable for insertion and deletion, thread-safe.
3.2 LinkedList
LinkedList is a linked list structure for storing data, it is suitable for data insertion and deletion in the dynamic random access and traverse slower. In addition, he also provides a method for the List interface not defined specifically for headers and footers operating elements, can be used as stacks, queues, queue and bidirectional use. Additions and deletions high efficiency, low query efficiency, thread safe
3.3 Vector
Vector and ArrayList, just as implemented by the array, except that it supports synchronous threads that only one thread at a time can write Vector, avoid multi-threading while writing the inconsistency caused, but synchronization requires a very high cost Therefore, it is slower than access access ArrayList. Higher query efficiency, low efficiency additions and deletions, thread-safe.
3.4 Stack
Last in, first out (LIFO) stack of objects. Inherited the Vector, it is an array. It is extended by the operation of the five classes Vector, allowing the stack to the vector considered.
Four points with relatively
4.1. Underlying data structure
ArrayList, Vector underlying rely on an array of high query efficiency, low deletions efficiency (because Vector is thread-safe, the overall efficiency is lower than the ArrayList)
LinkedList dependent on the underlying two-way circular linked list, additions and deletions high efficiency, low query efficiency
4.2 storage element aspects
ArrayList, Vector, LinkedList elements in an ordered, repeatable, allowing a null value
4.3 capacity expansion
ArrayList a 1.5-fold expansion
Vector according incremental expansion, in increments of 0, 2-fold expansion; otherwise the original capacity increment +
LinkedList no expansion
4.4. Thread safety
ArrayList, LinkedList thread safe (if there are multiple threads need to access the List of elements in the collection, consider using a collection Collections packaged into thread-safe collection)
Vector thread-safe
Fifth, face questions
5.1 ArrayList and the Vector similarities and differences:
Complement: supports fail-fast mechanism
Similarities and differences of 5.2 ArrayList and LinkedList
- Both are class that implements the interface List
- ArrayList data structure is based on a dynamic array, whereas LinkedList based linked list data structure
- For random access get and set (query), ArrayList LinkedList superior, to move the pointer because LinkedList
- For deletions operations (add and remove), LinkedList than ArrayList