Comparison of common Java collections

Source: Internet
Author: User

1. ArrayList

Array-based implementation without capacity restrictions.

You may need to expand the number of inserted elements. When deleting an element, the array capacity is not reduced.

To reduce the array capacity, you can call trimToSize ()

When searching for elements, You need to traverse the array and search for non-null elements in equals mode.

Non-thread security.

2. shortlist

Based on the two-way linked list mechanism.

Fast element insertion and movement.

Non-thread security.

3. Vector

Object array-based implementation.

Thread-safe ArrayList Based on synchronized.

The Capacity Expansion Mechanism for inserting elements is slightly different from that for ArrayList:
If capcacityIncrement> 0, the size of the Object array is expanded to the existing size plus capcacityIncrement;
If capcacityIncrement <0, the size of the Object array is doubled to the existing size;

4. Stack

Implemented based on Vector and supports LIFO.

5. HashSet

Implemented based on HashMap without capacity restrictions.

Duplicate elements are not allowed.

Non-thread security.

6. TreeSet

It is implemented based on TreeMap and supports sorting.

Non-thread security.

7. HashMap

The Entry object consisting of key and value is stored in arrays without capacity restrictions.

Search for the location where the Entry object is stored in the array based on key hash, and use the Linked List Method to Solve the hash conflict.

When an element is inserted, the array capacity may be increased. When the array capacity is increased, the hash is recalculated and the object is copied to the new array.

Non-thread security.

8. TreeMap

Implemented based on the red/black tree with no capacity limit.

Non-thread security.

-----------------------------------
Applicable scenarios:

Set or Map is a better choice for applications with frequent search and deletion and a large number of elements;

ArrayList is applicable to reading elements by position;

The sequence list is applicable to scenarios where you want to start or end operations or insert a specified position;

Vector is applicable to the scenario of ArrayList for thread security;

Stack is applicable to LIFO scenarios with thread security;

HashSet is suitable for storing non-repeating elements that do not require sorting;

TreeSet is suitable for storing non-repeating elements to be sorted;

HashMap is applicable to most key-value access scenarios;

TreeMap is applicable to key-value scenarios that require sorting and storage.

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.