17th Day of Java Learning (TreeSet HashSet)

Source: Internet
Author: User

Features of Set set (compared to ArrayList)
Unordered, unique

Two sub-classes that mainly study it

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

If you want to customize the object for HashSet traversal, then we need to rewrite the equals algorithm and the Hashcode method.

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

If you want to customize the object for TreeSet traversal, then we need to rewrite the CompareTo method.

17th Day of Java Learning (TreeSet HashSet)

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.