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