Differences and choices for Java collections

Source: Internet
Author: User

Collection

|--list Orderly, repeatable

|--arraylist

The underlying data structure is an array, query fast, and delete slowly.

Thread insecure, high efficiency

|--vector

The underlying data structure is an array, query fast, and delete slowly.

Thread-safe, low-efficiency

|--linkedlist

The underlying data structure is linked list, query slow, delete quickly.

Thread insecure, high efficiency

|--set disorder, the only

|--hashset

The underlying data structure is a hash table.

How to guarantee the uniqueness of the element?

Dependent on two methods: Hashcode () and Equals ()

These two methods can be generated automatically in development

|--linkedhashset

The underlying data structures are linked lists and hash tables

The chain list ensures that the elements are orderly

Guaranteed element unique by hash table

|--treeset

The underlying data structure is a red-black tree.

How do you ensure that elements are sorted?

Natural sort

Comparator sort

How to guarantee the uniqueness of the element?

Depends on whether the return value of the comparison is

How to choose

Is it the only one?

Yes: Set

Sort of?

Yes: TreeSet

No: HashSet

If you know it is set, but do not know which set it is, use HashSet.

No: List

Are you safe?

Yes: Vector

No: ArrayList or LinkedList

Query MORE: ArrayList

More additions and deletions: LinkedList

If you know it is list, but do not know which list, use ArrayList.

If you know it is collection collection, but do not know who to use, use ArrayList.

If you know how to use a collection, use ArrayList.

Differences and choices for Java collections

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.