On the HashSet Java-

1.HashSet

1.1 HashSet underlying data structure

  • HashSet underlying storage mechanism is to use a hash table

1.2 HashSet structure described in

HashSet bottom layer is a hash table, the hash table name suggests is a table, in which each cell is a bucket structure, the order of data is not stored at all, and the amount of data stored there is no clear limit. Each cell has a unique position index, but needs to be stored position data obtained by the calculation. Here need hashSet () method, to obtain a hash value of each object, and in accordance with the principles of the hash table is stored, which is calculated so that a cell stored at the current element location.

1.3 storage process

Here Insert Picture Description

  1. We will first call the object's hashCode () method to get the hash value of the object to be stored.
  2. The obtained cell is stored in the hash table element corresponding to the calculated hash value.
  3. Because each cell in the hash table is a bucket structure, the data stored no clear limit on the number, it is necessary to judge whether there are other elements present within the cell, if there is no other element is present, It can be stored directly in the cell.
  4. If there are other elements already present within the cell, you need to follow so that elements within the current cell comparison, the first to compare their hash values ​​are the same, if not the same, directly stored in the cell.
  5. If there are other elements of the hash value identical thereto within the cell, it will be called equals () method whether the contents are equal comparison, if the target content are not equal, it can be stored directly.
  6. If equals () method is equal to the comparison target content, indicating that the element has been repeated, is not stored in the hash table returns a false.
Released five original articles · won praise 5 · Views 209

Guess you like

Origin blog.csdn.net/cccccv_/article/details/104486804