HashMap details in Java

When can element in HashMap be lost?

Let's say that not a primitive is used as a key, but an object with several fields. After adding an element to the HashMap, one field of the object that acts as a key is changed, which is involved in calculating the hash code. As result, when trying to find a given element by the original key, the correct bucket will be accessed, but equals will no longer find the specified key in the list of elements. Nevertheless, even if equals is implemented in such a way that changing this field of the object does not affect the result, then after increasing the size of the buckets and recalculating the hash codes of the elements, the specified element, with the changed field value, with a high degree of probability will end up in a completely different bucket and then it will be completely lost.

Why can't byte[] be used as key in HashMap?

The hash code of an array does not depend on the elements stored in it, but is assigned when the array is created (the method for calculating the hash code of an array is not overridden and is calculated using the standard Object.hashCode() based on the address of the array). Equals are not overridden for arrays, and pointers are compared. This leads to the fact that access to the element saved with the key-array will not work when using another array of the same size and with the same elements, access can be done only in one case - when using the same reference to the array that was used to save element.

Role of equals() and hashCode() in a HashMap

hashCode allows you to define a bucket to search for an item, and equals is used to compare the keys of the items in the bucket list and the key to find.

Maximum number of hashCode() values

The number of values follows from the signature int hashCode() and is equal to the range of type int - 2^32.

Worst get(key) method running time for a key that is in a HashMap and isn't in a HashMap

O(N). The worst case is a search for a key in a HashMap, degenerated into a list due to the coincidence of keys by hashCode(), and to find out whether an element is stored with a specific key, it may be necessary to iterate over the entire list.

How many transitions occur at the time HashMap.get(key) is called on the key that is in the table?

  • the key is null: 1 - the only getForNullKey() method is executed.
  • any key other than null: 4 - calculation of the key hash code; determination of the bucket number; search for a value; return value.

How many new objects are created when you add a new item to the HashMap?

One new static nested class Entry<K,V>.

How and when does the number of buckets increase in the HashMap?

In addition to capacity, HashMap also has a loadFactor field, on the basis of which the maximum number of occupied buckets capacity * loadFactor is calculated. By default loadFactor = 0.75. Upon reaching the limit, the number of buckets is doubled and a new "location" is calculated for all stored items, taking into account the new number of buckets.

Meaning of the parameters in the constructor of HashMap(int initialCapacity, float loadFactor)

initialCapacity - the initial size of the HashMap, the number of buckets in the hash table at the time of its creation.

loadFactor - HashMap fill factor, when exceeded, the number of buckets increases and automatic rehashing occurs. It is equal to the ratio of the number of already stored elements in the table to its size.

Will HashMap work if all the keys being added have the same hashCode()?

Yes, it will, but in this case the HashMap degenerates into a linked list and loses its advantages.

How to iterate over all Map keys?

Use the keySet() method, which returns a set of Set<K> keys.

How to iterate over all Map values?

Use the values() method, which returns a Collection<V> of values.

How to iterate over all key-value pairs in a Map?

Use the entrySet() method, which returns a set of Set<Map.Entry<K,V>> key-value pairs.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed