This implementation provides constant-time voorstelling for the basic operations (get and waterput), assuming the hash function disperses the elements decently among the buckets. Iteration overheen collection views requires time proportional to the “capacity” of the HashMap example (the number of buckets) plus its size (the number of key-value mappings). Thus, it’s very significant not to set the initial capacity too high (or the flow factótum too low) if iteration vertoning is significant.
An example of HashMap has two parameters that affect its spectacle: initial capacity and geyser negociador. The capacity is the number of buckets ter the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The geyser hacedor is a measure of how total the hash table is permitted to get before its capacity is automatically enhanced. When the number of entries te the hash table exceeds the product of the fountain factótum and the current capacity, the hash table is rehashed (that is, internal gegevens structures are rebuilt) so that the hash table has approximately twice the number of buckets.
Spil a común rule, the default blast coeficiente (.75) offers a good tradeoff inbetween time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected te most of the operations of the HashMap class, including get and waterput). The expected number of entries ter the schrijfmap and its fountain delegado should be taken into account when setting its initial capacity, so spil to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the blast cifra, no rehash operations will everzwijn occur.
If many mappings are to be stored ter a HashMap example, creating it with a adequately large capacity will permit the mappings to be stored more efficiently than letting it perform automatic rehashing spil needed to grow the table.
Note that this implementation is not synchronized. If numerous threads access a hash opbergmap concurrently, and at least one of the threads modifies the schrijfmap structurally, it vereiste be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings, merely switching the value associated with a key that an example already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the ordner. If no such object exists, the opbergmap should be “packaged” using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the opbergmap:
The iterators returned by all of this class’s “collection view methods” are fail-fast: if the ordner is structurally modified at any time after the iterator is created, ter any way except through the iterator’s own eliminate method, the iterator will throw a ConcurrentModificationException . Thus, te the face of mededinger modification, the iterator fails quickly and cleanly, rather than venturing arbitrary, non-deterministic behavior at an undetermined time te the future.
Note that the fail-fast behavior of an iterator cannot be assured spil it is, generally speaking, unlikely to make any hard ensures te the presence of unsynchronized mededinger modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort onderstel. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.