Comparison of Java common ArrayList LinkedList collections

Source: Internet
Author: User
Tags hash

1. ArrayList

Implementation based on array, no capacity constraints.

When you perform an INSERT element, you may have to expand it, and deleting an element does not reduce the size of the array.

If you want to reduce the size of the array appropriately, you can call TrimToSize ()

When looking for an element, you want to enumerate the groups and look for a non-null element in the same way as equals.

Not thread safe.

2. LinkedList

Based on the bidirectional linked list mechanism implementation.

Elements are inserted and moved faster.

Not thread safe.

3. Vector

Based on the method of an object array.

Thread-safe ArrayList based on synchronized implementations.

The mechanism for capacity expansion is slightly different from the ArrayList when inserting elements:
If capcacityincrement > 0, the size of the object array expands to the existing size plus the capcacityincrement;
If Capcacityincrement < 0, the size of the object array expands to twice times the existing size;

4. Stack

Based on Vector implementations, supports LIFO.

5. HashSet

Based on HASHMAP implementation, no capacity limit.

Element duplication is not allowed.

Not thread safe.

6. TreeSet

Based on TREEMAP implementations, supports sorting.

Not thread safe.

7. HashMap

The entry object consisting of key and value is stored in array mode, without capacity restriction.

Based on the key hash to find the location of the entry object to the array, for the hash conflict using a linked list to solve.

The size of the array may be enlarged when the element is inserted, the hash is recalculated when the capacity is expanded, and the object is copied into the new array.

Not thread safe.

8. TreeMap

Based on the red-black tree implementation, no capacity constraints.

Not thread safe.

-----------------------------------
Applicable scenario:

A set or map is a better choice for applications that find and delete more frequently and have a larger number of elements.

ArrayList is suitable for scenes that read elements by location;

LinkedList is suitable for scenes that are to be operated or inserted at a specified position;

Vectors apply to ArrayList scenarios that are thread-safe;

Stack applies to thread-safe LIFO scenarios;

HashSet is suitable for storing non repetitive elements that are not required for sorting;

TreeSet applies to the storage of the distinct elements to be sorted;

HashMap is suitable for most key-value access scenes;

The

  TreeMap is suitable for key-value scenarios that need to be sorted and stored.

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.