Home
Recent Q&A
Java
Cloud
JavaScript
Python
SQL
PHP
HTML
C++
Data Science
DBMS
Devops
Hadoop
Machine Learning
Azure
Blockchain
Devops
Ask a Question
How does HashMap handle collisions in Java?
Home
DBMS
How does HashMap handle collisions in Java?
0
votes
asked
Aug 10, 2023
in
DBMS
by
DavidAnderson
How does HashMap handle collisions in Java?
dbms
Please
log in
or
register
to answer this question.
1
Answer
0
votes
answered
Aug 10, 2023
by
DavidAnderson
The
java.util.HashMap
class in Java uses the approach of chaining to handle collisions. In chaining, if the new values with the same key are attempted to be pushed, then these values are stored in a linked list stored in a bucket of the key as a chain along with the existing value.
In the worst-case scenario, it can happen that all keys might have the same hashcode, which will result in the hash table turning into a linked list. In this case, searching a value will take O(n) complexity as opposed to O(1) time due to the nature of the linked list. Hence, care has to be taken while selecting hashing algorithm.
...