In this article, I am revisiting a duo of interesting query related to the internal working of HashMap inward Java, by together with large asked senior Java developers, ranging from four to half dozen together with upwards to 8 years of experience. I did encompass lot of these questions from HashMap, ranging from thread-safety to race conditions, inward my postal service near internal working of Java HashMap, but I idea to revisit 2 of those questions, How acquire method of HashMap or Hashtable plant internally in Java together with What happens if 2 dissimilar keys provide the same hashCode, how create y'all provide value from HashMap inward that case. These are the question, which is highly pop inward investment banking domain, together with preferred selection of interviewer, piece interviewing experienced Java professional.
If these questions are however beingness asked, it agency non many are answering them inward the clarity together with confidence they are looking for. This motivates me to revisit these questions again. On a side note, inward fellowship to sympathize these questions, y'all should accept practiced cognition of equals together with hashcode method every bit well.
At to the lowest degree y'all should know that :
If these questions are however beingness asked, it agency non many are answering them inward the clarity together with confidence they are looking for. This motivates me to revisit these questions again. On a side note, inward fellowship to sympathize these questions, y'all should accept practiced cognition of equals together with hashcode method every bit well.
At to the lowest degree y'all should know that :
1) Two unequal objects may provide the same hashcode.
2) When 2 objects are equal past times equals(), they must accept the same hashcode.
How acquire method of Hashtable plant inward Java
HashMap, Hashtable, together with ConcurrentHashMap.
Another optional, but worth mentioning requirement of keys inward a hash-based collection is beingness an Immutable object. Keeping this cognition along amongst full general cognition of hashing algorithm, which revolves simply about hash function.
You should read a proper majority on information construction together with algorithms to larn to a greater extent than near how a hash business office plant together with dissimilar strategies to handgrip collisions inward Java. One of such majority is Introduction to Algorithms past times Thomas Cormen, perfect for whatsoever programmer interested inward learning to a greater extent than near algorithms together with information structures.
Anyway, let's run across those HashMap questions in i lawsuit again :
Another optional, but worth mentioning requirement of keys inward a hash-based collection is beingness an Immutable object. Keeping this cognition along amongst full general cognition of hashing algorithm, which revolves simply about hash function.
You should read a proper majority on information construction together with algorithms to larn to a greater extent than near how a hash business office plant together with dissimilar strategies to handgrip collisions inward Java. One of such majority is Introduction to Algorithms past times Thomas Cormen, perfect for whatsoever programmer interested inward learning to a greater extent than near algorithms together with information structures.
Anyway, let's run across those HashMap questions in i lawsuit again :
1) How does get(Key key) method plant internally inward HashMap, together with Hashtable inward Java?
Here are steps, which happens, when y'all telephone holler upwards get() method amongst fundamental object to call upwards corresponding value from hash based collection
a) Key.hashCode() method is used to notice the bucket place inward backing array. (Remember HashMap is backed past times array inward Java) Though hashcode() is non used directly, but they are passed to internal hash() function.
b) In backing array or meliorate known every bit the bucket, fundamental together with values are stored inward the shape of a nested shape called Entry. If at that spot is solely i Entry at bucket location, thus the value from that entry is returned. Pretty straightforward right?
Things acquire fiddling tricky, when Interviewer inquire the instant question, What happens if 2 keys accept the same hashCode? If multiple keys accept the same hashCode, thus during put() performance collision had occurred, which agency multiple Entry objects stored inward a bucket location. Each Entry keeps rail of some other Entry, forming a linked listing information structure there.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt ConcurrentHashMap together with HashMap inward Java
how HashMap together with LinkedHashMap handgrip collision inward Java to larn to a greater extent than near this change.
Now, if nosotros demand to call upwards value object inward this situation, next steps volition last followed :
Now, if nosotros demand to call upwards value object inward this situation, next steps volition last followed :
1) Call hashCode() method of the fundamental to finding bucket location.
2) Traverse idea linked list, comparison keys inward each entries using keys.equals() until it returns true.
So, nosotros utilization equals() method of a fundamental object to notice right entry together with thus provide value from that. Remember key.equals() method, together with this is what Interviewer desire to know. I accept seen many programmers mentioning value.equals(), which may last due to interview nervousness, but that’s incorrect. Since y'all don't accept value object passed to get() method, at that spot is no query of calling equals together with hashCode method on value object.
That's all on these 2 HashMap questions guys. Remember to call near key.hashCode() together with key.equals(), whenever mortal asks how to acquire method of HashMap or Hashtable plant inward Java. H5N1 value object is non used, it simply exists inward Entry thus that acquire tin provide it.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt ConcurrentHashMap together with HashMap inward Java
No comments:
Post a Comment