How hashcode is equal is implemented in hashmap

What are hashtable and hashmap?

Starting with Java 1.2, this was supplemented by the to correspond to the framework. Both are implementations of a hash table that are very similar. Oracle recommends using, but unlike, it is not synchronized.
Both store key-value pairs, the keys of which are always unique. When setting a new value with an existing key, its value is overwritten. In principle, the key and value can be objects of any type, which must, however, implement the methods and. allowed, but prohibits entries. Primitive data types are not permitted and are used as a key or value when they are set Boxing converted into objects of the corresponding wrapper classes.

And constructors have two parameters that are decisive for the efficiency and speed of their work: the initial capacity (initial capacity) and you load factor (Fill factor). If both values ​​are not set in the constructor, the initial capacity for the 11, for the 16 value pairs, with a fill factor of 75% each (0.75). The fill factor indicates the maximum extent to which the collection can be filled. If this value is exceeded when inserting new elements, it is newly created and calculated internally. The required overhead of the collection has to do with the internal conversion of the entries into an array and the handling of collisions between double hash values1 to do and is crucial in speed-critical applications.
For a less time-critical use, it is advisable not to change the fill factor. This ensures the most efficient compromise between speed and storage requirements.

public class HashMapExample {public static void main (String [] args) {HashMap ht = new HashMap (); String s; ht.put (2, "Gilera"); ht.put (1, "Moto Guzzi"); ht.put (3, "Morbidelli"); System.out.println ("Pass 1:"); for (Integer elem: ht.keySet ()) {s = ht.get (elem); System.out.println (elem + "-" + s); } // Value under key "1" is replaced ht.put (1, "Ducati"); System.out.println ("\ nPass 2:"); Set keys = ht.keySet (); Iterator iterator = keys.iterator (); while (iterator.hasNext ()) {Integer i = (Integer) (); s = ht.get (i); System.out.println (i + "-" + s); } System.out.println ("\ nMoto Guzzi available?" + Ht.containsValue ("Moto Guzzi")); ht.put (4, "Moto Morini"); System.out.println ("\ nNumber of elements after inserting '4':" + ht.size ()); System.out.println ("\ nPass 3:"); Collection vals = ht.values ​​(); Iterator iter = vals.iterator (); while (iter.hasNext ()) {System.out.println ( ()); } ht.remove (4); System.out.println ("\ nNumber of elements after removing '4':" + ht.size ()); ht.clear (); System.out.println ("\ nNumber of elements after clear ():" + ht.size ()); }}

Some common ways of using a are demonstrated in the example.
This is a generic type for which it must be specified which types are used for specifying the key and value. In the example these are for the key and for the value. It is possible to specify primitive int values ​​when inserting with, as these are converted into, as explained above.
The method can be used when querying all entries. It provides an all key that can be looped through. By passing the individual key as a parameter to the method, its associated value can then be accessed.

After replacing the value under key '1', in addition to the extended for loop used above, a second way of going through the key set is demonstrated. An iterator object is created by the method. Its method checks whether further elements are present and can thus be used as a termination condition in a loop. There the next available element - here the next key - is also determined.
A is also used in the third and last loop, but it now runs through the values ​​determined with.

By transferring the key to the method, an entry can be removed from the; this can be completely emptied.

1) Duplicate hash values ​​can arise internally during the calculation from the keys of the entries. If they occur, they are handled by special collision mechanisms.