One, Javase (17) Set set, collection set, for collection collection who we use, the data structures common in the collection

Source: Internet
Author: User
Tags set set

1: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 Fa1se: 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 ()

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

C: Look at the code we've talked about.

(4) Case

A: Get random numbers with no duplicates

B: Keyboard Entry students follow the total score from the high end output

2:collection Collection Summary

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

What guarantees the element uniqueness?

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?

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's list, but you don't know which list it is, 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, just use ArrayList.

5: Data Structures common in the collection (master)

ARRAYXXX: The underlying data structure is a number of groups, 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 Equa1s ()

TREXXX: The underlying data structure is two tree, two ways of sorting; natural sorting and comparator sorting

One, Javase (17) Set set, collection set, for collection collection who we use, the data structures common in the collection

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.