Java Basics (vii)---set

Source: Internet
Author: User

Set

(1) Features of Set set
Unordered (deposit and Fetch order), unique


(2) HashSet Collection
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 ()

Note: If you need to save an object of a class to the HashSet collection, overriding the Equals () method and the Hashcode () method is to ensure that the return value of the hashcode is true when the object returns a value of true through the Equals () method;

Each "slot" slot in HashSet that can store an element is often referred to as the bucket bucket, and the Equals () method returns False if there are multiple elements with the same hashcode value. You need to put multiple elements in a "bucket", which can cause performance degradation;

(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

Java Basics (vii)---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.