Java Data Structure Learning

Source: Internet
Author: User

Set
Unordered and non-repeated traversal can only use the iterator or enhance the for Loop (equivalent to the java compiler ).
1) hashSet: implemented using a hash table;
2) TreeSet: Implemented by sorting binary trees;
When using the contains method to determine whether an object is included in a set, you must first index the object to a specific space based on its hashCode value, and then compare it with the object in the space by calling the equals method. This targeted search method is more efficient than linear table comparison.
For objects that override the equals method, we generally need to properly override the hashCode method () inherited from the object class. Otherwise, two objects that may have equal values will be returned as false after comparison of equals. For example:
Point p1 = new Point (1, 2 );
Point p2 = new Point (1, 2 );

If the hashCode method is not properly written, and the hashCode values of p1 and p2 may differ, there is no chance to compare them.


To be continued...


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.