Collection summary of the collection and if appropriate collections are selected (including comparisons to list and set)

Source: Internet
Author: User

Diagram of 1:collection

2:set Collection (understanding)
(1) Features of Set set
Unordered, unique
(2) HashSet Collection (Master)
A: The underlying data structure is a hash table (is an array of elements as linked lists)
B: Hash table bottom-dependent two methods: Hashcode () and Equals ()
Execution order:
First, compare whether the hash value is the same
Same: Continue to execute equals () method
Returns true: The element is duplicated, not added
return false: Add elements directly to the collection
Different: Add elements directly to the collection
C: How to guarantee the uniqueness of the element?
Guaranteed by Hashcode () and Equals (), call Hashcode () before calling Equals ().
D: When developing, the code is very simple and can be generated automatically.
E:hashset Store strings and traverse
F:hashset Store a custom object and traverse it (the same element is the same as the object's member variable value)
(3) TreeSet Collection
A: The underlying data structure is a red-black tree (a self-balancing two-fork tree)
B: Guaranteed Ordering of elements
A: Natural sorting (elements are comparative)
Implementing the comparable interface for the class to which the element belongs
B: Comparator sorting (collection with comparison)
Let the collection construct method receive comparator implementation class object


3:collection Collection Summary (master)
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 Unordered, unique
|--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

4: For collection collection who do we use? Master
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.

5: Data Structures common in the collection (master)
ARRAYXXX: The underlying data structure is an array, query fast, and delete slowly
LINKEDXXX: The underlying data structure is linked list, query slow, and delete quickly
HASHXXX: The underlying data structure is a hash table. Dependent on two methods: Hashcode () and Equals ()
TREEXXX: The underlying data structure is a two-fork tree. Sort by two ways: natural sort and comparator sort


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Collection summary of the collection and if appropriate collections are selected (including comparisons to list and set)

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.