Here is some explenation on what goes on under the hood.
- The insertion of elements in a HashMap is not dependent on the order in which they are added. Instead, it relies on the hash code of the key and the availability of slots in the map’s internal array.
- If the computed slot for a key is already occupied (due to a hash collision), the HashMap resolves this by either chaining or probing. This resolution process does not preserve insertion order and can involve traversing or modifying multiple slots.
- When the map’s capacity is exceeded, a new, larger map is created, and all existing entries are rehashed and redistributed. This rehashing process ignores the original insertion order entirely.