Set set and Implementation class

Source: Internet
Author: User

1 HashSet is the main implementation class, and the methods commonly used in set are defined under collection.

2 Disorder: Disorder! = Randomness. The position of the element in the underlying storage is unordered

Non-repeatability: When adding the same element, the following elements cannot be added

3 Note: To add elements to the set, be sure to override the Equals and Hashcode methods

To guarantee the non-repeatability of the elements in the set.

4 How are elements stored in set? Using a hashing algorithm, like arranging students to sit in different places in the classroom, the elements are stored in different places in memory.

5 When you add an object to a set, you first call the Hashcode () of the class that contains the object, and the hash value of this object determines where the object is stored in set. If there is no object storage at this location, then this object is stored directly in this location, if there is already an object store at this location, and then by Equals () compares two objects are the same, if the same, the subsequent elements cannot be added.

Requirements: Hashcode () method values are consistent with the Equals method.

Set set and Implementation class

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.