Chapter 4 Comparison and summary of the four List Implementation classes, Chapter 4 list
1. ArrayList
- Non-thread security
- Object-based array
- Get (int index) does not need to traverse the array, which is fast;
- Get (int index) is called in the iterator () method, so the speed is fast.
- Set (int index, E e) does not need to traverse the array, fast
- The add method requires consideration of resizing and array replication, which is slow.
- Removing (Object o) requires traversing the array and copying array elements, which is slow
- Remove (int index) does not need to traverse the array. You need to copy the array elements, but it is not commonly used.
- Contain (E) needs to traverse the Array
2. shortlist
- Non-thread security
- Based on a circular two-way linked list
- Get (int index) requires a traversal table, which is slow;
- The iterator () method calls get (int index), so the speed is also slow.
- Get (int index) is called in the set (int index, E) method, so the speed is also slow.
- The add method does not need to consider the expansion and array replication issues. You only need to create a new object, and then direct the pointer of the front and back nodes of the new object to the reallocation, which is fast.
- Remove (Object o) requires a traversal table, but does not need to copy elements, you only need to direct the pointer of the front and back nodes of the object to be deleted to the reallocation and leave the three attributes of the object to be deleted empty. This is fast.
- Remove (int index) requires a traversal table, but does not need to copy elements, you only need to direct the pointer of the front and back nodes of the object to be deleted to the reallocation and leave the three attributes of the object to be deleted empty.
- Contain (E) requires a time series table
3. Vector (thread-safe ArrayList)
- Thread Security
- The expansion mechanism is different from that of ArrayList.
4. Stack (inherited from Vector)
- Thread Security
- Low Efficiency: Deque or shortlist can be used for dual-end queues.
Summary:
- ArrayList
- When add or remove is used more often, use the revoke list
- When thread security is required and efficiency requirements are relatively low, there are many ways to implement ArrayList thread security by using Vector.
- If you need to use the Stack structure, use Deque to discard the Stack.