Java Basics-Set of 2Set interfaces and map interfaces

Source: Internet
Author: User

First speaking Set

An overview

Set:1. Element Storage has no subscript, so elements are unordered ( the order in which they are deposited and fetched is not necessarily consistent

2. Elements cannot be duplicated

|--hashset: The underlying data structure is a hash table . Thread is out of sync. The principle of guaranteeing element uniqueness is to determine whether the hashcode value of an element is the same. If it is the same, it continues to judge whether the element's Equals method is true.

|--treeset: You can sort the elements in the set collection. The default is to sort alphabetically by nature. The underlying data structure is a two-fork tree. The basis for guaranteeing the uniqueness of the element: CompareTo method return 0.

The set set has no unique functionality, and the function of the set set is consistent with the collection

Two HashSet

HashSet: Thread is unsafe, access speed is fast.

HashSet principle: HashSet can not be stored in duplicate values, if Add () stored in duplicate values, return false, then hashset how to ensure that the data is not unique?

The hashing algorithm increases the efficiency of de-duplication and reduces the number of uses of the Equals () method if it is less efficient when compared to equals () at the time of storage.

Element uniqueness can be ensured through the two methods of the element, hashcode and equals. If the hashcode value of the element is the same, the equals is not determined to be true. If an element has a different hashcode value, equals is not called.

2. After knowing the hashset principle, we must override the Hashcode () and Equals () methods when storing custom classes

* Hashcode (): The return value of the same object must be the same, the return value of the property is different (improve efficiency)

* Equals (): property returns True, different property returns False, returns False when storing

Java Basics-Set of 2Set interfaces and map interfaces

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.