How hashmap works in Java. My style of learning.

  •        0
  

This is the most frequently asked questions in the interview. Googling will throw many links related to this topic. How to learn the implementation of hash map? My style of learning using open source learning technique.

Download Java code from openjdk. Open the source of HashMap.Java. The file is available under src\java\util folder. Check out how it is implemented.

  • HashMap is not synchronized. There is no synchronized keyword available in source.
  • It accepts null value. There is a explicit check in get and put function, which checks for null and creates a separate value for it.
  • Every object in Java has hashcode. HashMap has a hash function which generates hash value of hashcode. This value is used as index to retrieve object from the bucket.
  • It stores both key and value in the bucket as Map.Entry.
  • While retrieving, it identifies the bucket using the hash value and loops through all the values. It returns the value which matches the key. Since it is storing both key and value, it will retrieve the correct object even two object generates the same hash code.
  • If the size of the bucket reaches the threshold then it resizes the bucket and transfers the current objects to it. This operation is expensive so plan ahead for better capacity by passing it as constructor argument.
This method of learning is called Open Source Learning Technique. If you follow, it will certainly make you independent. Googling will not always give you correct result. You need to open up 10 links instead, spend some time in reading code.

Happy learning !!


   

comments powered by Disqus


Related Articles

How to contribute to open source

  • opensource contribute

I could see many many students posting this question in many forums, I want to contribute to open source but How to contribute? There are many ways to do that. I have listed a few and I hope it might be useful.

Read More


How to learn from open source projects

  • open-source learning methodology

Students ask this question frequently steps or methodology to learn from open source projects. There is no single answer or steps available. I listed the steps which i follow and i hope this will help for few.

Read More


Benefits in contributing to Open Source

  • open-source opensource contribute benifits

What the benefit will i get, if i contribute to Open Source? This is the frequently asked question by many people. I just want to pen down the benefits which i know and i hope you will agree with it.

Read More


Solr vs Elastic Search

  • full-text-search search-engine lucene solr elastic-search

Solr and Elastic Search are built on top of Lucene. Both are open source and both have extra features which makes programmer life easy. This article explains the difference and the best situation to choose between them.

Read More


How to solve CommunicationsException in Java while using Hibernate and MySQL

  • java hibernate mysql communicationsexception timeout

You might have faced CommunicationsException, basically timeout issue in Java while connecting to MySQL using Hibernate. The session would be timed out after certain period of time. You might be thinking that the your site or application is running without any issue but it would have stopped or crashed due to exception.

Read More


8 Best Open Source Searchengines built on top of Lucene

  • lucene solr searchengine elasticsearch

Lucene is most powerful and widely used Search engine. Here is the list of 7 search engines which is built on top of Lucene. You could imagine how powerful they are.

Read More


Lucene Vs Solr

  • searchengine lucene solr

Lucene is a search library built in Java. Solr is a web application built on top of Lucene. Certainly Solr = Lucene + Added features. Often there would a question, when to choose Solr and when to choose Lucene.

Read More


Free Open Source Code Search Engines

  • code-search code-search-engine search-engine

There are couple of sites which indexes the open source code and provides support to search code. Recently Google announced that they removed code search support from Google code. This article provides pointer for code search engine sites.

Read More


Identicon - Get unique avatar for your website users

  • identicon avatar icon gravatar

Users login in to the Websites, Blogs, Forum and adding their content, comments, asking or answering the questions etc. If user has added his photo then it will get displayed besides his content but not everyone provides his photo image to the sites. Most of the sites used to display a common avatar image for those who have not provided their image. When we read the content, it may be odd to see the same image representation for all users. Identicon comes for the rescue.

Read More


An introduction to LucidWorks Enterprise Search

  • lucene solr search engine enterprise

Lucidworks Enterprise search solution is built on top of Apache Solr. It scales seamlessly w/sub-second response times under extreme query loads for multi-billion document collections. It has user friendly UI, which does all the job of configuration and search.

Read More