Collection collection summary notes, Collection collection Collection notes

Source: Internet
Author: User

Collection collection summary notes, Collection collection Collection notes

2: Set (understanding)
(1) features of the Set
Unordered, unique
(2) HashSet set (master)
A: The underlying data structure is A hash table (an array whose elements are linked lists)
B: The underlying layer of the hash table depends on two methods: hashCode () and equals ()
Execution sequence:
First, compare whether the hash value is the same
Same: continue to execute the equals () method
Returns true. The element is repeated and is not added.
False: add the element to the set directly.
Different: add the element directly to the set.
C: how to ensure the uniqueness of elements?
Guaranteed by hashCode () and equals ()
D: during development, the code is very simple and can be automatically generated.
E: HashSet stores strings and traverses them.
F: HashSet stores and traverses custom objects (the same element is the same when the object's member variables are the same)
(3) TreeSet
A: The underlying data structure is A red/black tree (A self-balancing Binary Tree)
B: ensure that elements are sorted.
A: Natural sorting (comparison of elements)
Implement the Comparable interface for the class to which the element belongs
B: comparator sorting (set comparison)
Allows the set constructor to receive Comparator implementation class objects
C: Just read the code we mentioned.
(4) case:
A: Obtain random numbers without duplicates.
B: The keyboard entry is output from the top to the bottom according to the total score.

3: Collection Summary (master)
Collection
| -- The List is ordered and can be repeated.
| -- ArrayList
The underlying data structure is an array, which provides fast query and slow addition and deletion.
Low thread security and High Efficiency
| -- Vector
The underlying data structure is an array, which provides fast query and slow addition and deletion.
Thread security and low efficiency
| -- Upload list
The underlying data structure is a linked list, which is slow in query and fast in addition and deletion.
Low thread security and High Efficiency
| -- Set unordered, unique
| -- HashSet
The underlying data structure is a hash table.
How can we ensure the uniqueness of elements?
Two methods are required: hashCode () and equals ()
The two methods are automatically generated during development.
| -- Javashashset
The underlying data structure is linked list and hash table.
Ensure element order by linked list
Ensure that elements are unique in a hash table
| -- TreeSet
The underlying data structure is a red/black tree.
How can we ensure the sorting of elements?
Natural sorting
Comparator sorting
How can we ensure the uniqueness of elements?
Determines whether the returned value is 0.

4: Who are we using for Collection sets? (MASTER)
Unique?
Yes: Set
Sort?
Yes: TreeSet
No: HashSet
If you know the Set but do not know which Set it is, use HashSet.

No: List
Want security?
Yes: Vector
No: ArrayList or revoke list
Multiple queries: ArrayList
Add, delete, and more: Upload list
If you know the List but do not know which List it is, use ArrayList.

If you know the Collection, but you do not know who to use, use ArrayList.

If you know that a set is used, ArrayList is used.

5: Common Data Structures in the collection (master)
ArrayXxx: the underlying data structure is an array, which provides fast query and slow addition and deletion.
Invalid XXX: the underlying data structure is a linked list. Slow query and fast addition and Deletion
HashXxx: the underlying data structure is a hash table. Two methods are required: hashCode () and equals ()
TreeXxx: the underlying data structure is a binary tree. Two sorting methods: Natural sorting and comparator sorting

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.