Chapter 4 Comparison and summary of the four List Implementation classes, Chapter 4 list

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.