Hashset, iterator

Source: Internet
Author: User

The hashset class is mainly designed for performing high-performance set operations, such as intersection, union, and difference sets for two sets. Set contains a group of elements that do not repeat and have no feature order.

(1) Some features of hashset are as follows:

1. The values in hashset cannot be repeated and there is no sequence.

2. The capacity of the hashset is automatically added as needed.

 

(2) How does an hshset ensure that the values in the set are not repeated?

When hashset is used, the hashcode () method is called, it first checks whether the hash code value of the object already stored in the set is consistent with the hash code value of the newly added object. If the hash code value is inconsistent, it is directly added to the Set; if they are the same, compare the equals method. If the equals method returns true, it indicates that the object already exists, so it is not added. Otherwise, it is added.

 

(3) There is no get () method in hashset, so we need to use the iterator.

 

The iterator mode is also called the cursor mode:

Provides a method to access each element in a container object without exposing the internal details of the object. From the definition, we can see that the iterator mode is generated for containers. Obviously, the container object here is hashset.

There are two main methods used:

A) hasnext () method: determines whether the container has the next element. If yes, true is returned. If no element exists, false is returned.

B) Next () method: extracts the next element.

 

Then, how can I use an iterator to output the content of hashset? Let's look at the following code:

The above code generates an iterator object. The while structure uses the hasnext () method to traverse the elements of the hashset and uses the next () method to access the object.

 

Hashset, iterator

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.