Performance Improvement Of HashMap In Java 8

Posted By : Lokesh Kumar | 30-May-2018

HashMap<K, V> is a quick, versatile and pervasive information structure in every Java program. To begin with a few nuts and bolts. As you likely know, it utilizes hashCode() and equals() strategy for keys to part values between buckets. The number of buckets (containers) ought to be somewhat higher than the number of sections in a guide with the goal that each bucket holds just a couple (ideally one) value. When looking up by key, we very quickly decide bucket (utilizing hashCode()modulo number_of_buckets) and our thing is available at the constant time.

This ought to have just been known to you. You likely additionally know that hash collisions have the sad effect on HashMap execution. At the point when numerous hashCode() values wind up in a similar bucket, values are set in an impromptu linked list. In the most pessimistic scenario, when all keys are mapped to the same bucket, along these lines worsening hash guide to the linked list - from O(1) to O(n) lookup time.

Hash collision debases the execution of HashMap fundamentally. With this new approach, existing applications can expect execution improvements in the event that they are utilizing HashMaps having an expansive number of components by just moving up to Java 8.
Hash collisions have a negative effect on the lookup time of HashMap. At the point when various keys wind up in a similar bucket, at that point values alongside their keys are put in a linked list. If there should arise an occurrence of retrieval, the linked list must be traversed to get the passage. In most dire outcome imaginable, when all keys are mapped to the same bucket, the lookup time of HashMap increments from O(1) to O(n).

Java 8 has accompanied the accompanying improvements/changes of HashMap objects if there should be an occurrence of high collisions.

  • The alternative String hash function included Java 7 has been removed.
  • Buckets containing an expansive number of impacting keys will store their entrances in a balanced tree rather than a linked list after a specific limit is come to.

Above changes to guarantee the execution of O(log(n)) in most pessimistic scenario situations (hash function isn't conveying keys appropriately) and O(1) with legitimate hashCode().

How is linked list replaced with the binary tree?

In Java 8, HashMap replaces linked list with a binary tree when the quantity of components in a bucket achieves the specific edge. While converting the list to the binary tree, the hashcode is utilized as a branching variable. On the off chance that there are two distinctive hashcodes in a similar bucket, one is viewed as greater and goes to one side of the tree and another to one side.

In any case, when both the hashcodes are equivalent, HashMap accept that the keys are comparable, and compares the key to decide the bearing with the goal that some request can be kept up. It is a decent practice to make the keys of HashMap comparable. HashMap trusts that the keys are Comparable so it can build up some order. This isn't a necessity of HashMap keys, yet clearly a decent practice. In the event that keys are not comparable, don't expect any execution improvements if there should arise an occurrence of heavy hash collisions.

This JDK 8 change applies just to HashMap, LinkedHashMap and ConcurrentHashMap.

Why is all of this so important?

This is for the most part security-related change. While in ordinary circumstance it's infrequently conceivable to have numerous collisions, if hash keys arrive from an untrusted source (e.g. HTTP header names received from the client), at that point it's conceivable and not very hard to extraordinarily craft the information, so the subsequent keys will have the same hashcode. Now if you perform many lookups, you may experience denial-of-service. Over and over getting to such keys will fundamentally affect server execution, effectively bringing about the denial-of-service attack.

JDK 8 a stunning bounce from O(n) to O(log(n)) will prevent such attack vector, likewise making execution somewhat more predictive.

Thanks.

 

About Author

Author Image
Lokesh Kumar

Lokesh is a Software Developer having experience in Core Java, Hibernate, Struts, MongoDB. His interest includes playing chess,cricket,badminton.

Request for Proposal

Name is required

Comment is required

Sending message..