0

I came across following paragraph on this page:

Prior to Java 8, HashMap and all other hash table based Map implementation classes in Java handle collision by chaining, i.e. they use linked list to store map entries which ended in the same bucket due to a collision. If a key end up in same bucket location where an entry is already stored then this entry is just added at the head of the linked list there. In the worst case this degrades the performance of the get() method of HashMap to O(n) from O(1). In order to address this issue in the case of frequent HashMap collisions, Java8 has started using a balanced tree instead of linked list for storing collided entries. This also means that in the worst case you will get a performance boost from O(n) to O(log n).

It sounds very interesting, however I was guessing what could be the source of this information? I know official Oracle tutorials can be found here. But I dont think they go in so much depth. Even HashMap api docs does not explain the same. Can you point me to any official Oracle docs / tutorials which explain this and such in depth facts? Or the source of this information is open source code?

MsA
  • 2,599
  • 3
  • 22
  • 47
  • 2
    "Or the source of this information is open source code?" indeed it is – Federico klez Culloca Mar 09 '20 at 10:39
  • If you're curious, [here](https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/HashMap.java) is the Java 8 source for `HashMap`. – Federico klez Culloca Mar 09 '20 at 10:41
  • 3
    "*Even HashMap api docs does not explain the same*" That's because it's an implementation detail. If you care about implementation details, the source is the source code. – Michael Mar 09 '20 at 10:42

1 Answers1

0

HashMap works on the principle of hashing, we have put(key, value) and get(key) method for storing and retrieving Objects from HashMap. When we pass Key and Value object to put() method on Java HashMap, HashMap implementation calls hashCode method on Key object and applies returned hashcode into its own hashing function to find a bucket location for storing Entry object, important point to mention is that HashMap in Java stores both key and value object as Map.Entry in a bucket which is essential to understand the retrieving logic.

Akhil Pathania
  • 542
  • 2
  • 5
  • 19