HASHMAP IN JAVA


About author  I am Java/oracle professional.Working on Java/J2EE technologies and i.e Java,J2ee,Oracle ADF,hibernate,J2ee,PL/sql,Apps for 4+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion on vinay@techartifact.com http://www.linkedin.com/in/vinaykumar2 Read more from this author


A map is an interface in the java.util package which stores the association between key and its corresponding value. A map cannot contain duplicate keys; each key can map to at most one value Map differs from array in a way that we can store a value at a particular index in arrays but a Map determines the index itself and does this based on the value on the key.
HASHMAP is a class which implements the map interface.
Hash map is a data structure which uses a hash function to map identifying values, known as keys (e.g., a person’s name) to their associated values (e.g., their telephone number).
The hash function transforms the key into the index (the hash) of an array element where the corresponding value is to be sought.
Take for instance the case of a phonebook. you can have a map where the keys are phone numbers and the value is the name of the person..
Given a key you can find its value.
Ex.[“key”, “value”]=[“88028”,”vinay”]
With the given key value i.e 88028 one can find its value….. which is vinay.
Take another example…..

import java.util.Map;
import java.util.HashMap; 

public class Map_example {
Map<Integer, String> addPhoneNum = new HashMap<Integer, String>();
public void addPhoneNum(Integer phonenumber, String name) {
addPhoneNum.put(phonenumber, name);
}
public String getName(Integer phonenumber) {
return addPhoneNum.get(phonenumber);
}
 public static void main(String[] args) {
Map_example m1 = new Map_example();
m1.addPhoneNum(9911538992 "vinay"); 

System.out.println(
m1.getName(9911538992)); 

} 

Main features of HASHMAP are.
• HashMap is a library class in Java.
• It is already implemented, hence can be used immediately HashMap is a library class in Java.
• HashMap stores only object references. That’s why, it’s impossible to use primitive data types like double or int. Use wrapper class (like Integer or Double) instead.
• For multi-theaded(synchronized) array class use Hashtable (java.lang.Hashtable)

The two most important HashMap’s methods are:
• get( Object key ) –this method returns the value associated with specified key in this hash map, or null if there is no value for this key
• put(K key, V value) – this method associates the specified value with the specified key in the map.

Internally a HashMap maintains an array. To have the HashMap work efficiently, the array must be large enough so that the key/value pairs are well-distributed and the performance is not affected. Therefore, the HashMap maintains two (customizable) variables :
1. Capacity
2. LoadFactor.
The capacity is the length of the internal array whereas the LoadFactor controls when the capacity should be increased.

24 Responses to HASHMAP IN JAVA


  1. Peter LawreyNo Gravatar
    Jul 03, 2010

    Hi, A very useful post for those new to Maps. However, one correction.

    For multi-theaded(synchronized) array class use Hashtable (java.lang.Hashtable)

    HashMap and Hashtable are Maps, not arrays.

    Hashtable is in the java.util package like HashMap.

    IMHO Hashtable has been a legacy class since JDK 1.2. If you Have JDK 1.2, 1.3 or 1.4 Use Collections.synchronizedMap(new HashMap());

    However if you have JDK 5.0 or 6 (available since ’04) I suggest trying ConcurrentHashMap which is much improved.


  2. Rick RykerNo Gravatar
    Jul 06, 2010

    When using HashSet, HashMap, or any of the implementation class that uses a hashCode for internal lookups, you must ensure that the classes of objects you place into the HashMap have a deterministic hash code, one that will not change once an instance has been added to the set or to the key set of the map.


  3. VenkatNo Gravatar
    Sep 21, 2010

    Hi Rick, that is a good idea which you have mentioed. If my understanding is correct, what you mean is to override the hashCode() method in the class which is going to be used as key.


  4. Kefaleas StavrosNo Gravatar
    Oct 01, 2010

    Nice and clear description!
    Well Done!


  5. Javin PaulNo Gravatar
    Jan 06, 2011

    Hi,

    HashMap is indeed very useful class and proper knowledge of it along with its counterpart hashtable is very important. here is some point worth looking.

    1. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls).
    2. HashMap does not guarantee that the order of the map will remain constant over time.
    3. HashMap is non synchronized whereas Hashtable is synchronized.
    4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn’t.

    to read more about Hashtable vs HashMap see here.


  6. Javin @ Tibco RV TutorialNo Gravatar
    Feb 02, 2011

    Hi,
    Thanks for this Nice artilce just to add while discussing about HashMap its worth mentioning following questions which frequently asked in Java interviews now days like How HashMap works in Java or How get() method of HashMap works in JAVA very often. on concept point of view these questions are great and expose the candidate if doesn’t know deep details.

    Javin
    FIX Protocol tutorial


  7. projektowanie oświetleniaNo Gravatar
    Feb 15, 2011

    Nice and very interesting post. Your point of view is more or less the same as main. Thanks!


  8. projektowanie oświetleniaNo Gravatar
    Feb 15, 2011

    It is truely very good post, but I do not see everything completely clear, especially for someone not involved in that topic. Anyway very interesting to me.


  9. oprawy oświetlenioweNo Gravatar
    Feb 15, 2011

    It is really very good post, but I do not see everything completely clear, especially for someone not involved in that topic. Anyway very interesting to me.


  10. budowa basenówNo Gravatar
    Feb 15, 2011

    I think this is one of the most important info for me. And i’m glad reading your article. But want to remark on few general things, The website style is perfect, the articles is really nice : D. Good job, cheers


  11. Przeprowadzki WarszawaNo Gravatar
    Feb 16, 2011

    Good writing never goes out of style!


  12. PrzeprowadzkiNo Gravatar
    Feb 16, 2011

    Thank you for another informative blog. Where else could I get that kind of information written in such an ideal way? I’ve a project that I am just now working on, and I have been on the look out for such information.


  13. Przeprowadzki WarszawaNo Gravatar
    Feb 16, 2011

    Good Web-Site, keep going !!!!


  14. Stomatologia Estetyczna KrakówNo Gravatar
    Feb 16, 2011

    Hi, Neat post. There is a problem with your website in internet explorer, would check this… IE still is the market leader and a big portion of people will miss your fantastic writing because of this problem.


  15. Przeprowadzki WarszawaNo Gravatar
    Feb 17, 2011

    Simply amazing article!!!


  16. tanio przeprowadzkiNo Gravatar
    Feb 17, 2011

    We are a group of volunteers and starting a new scheme in our community. Your web site offered us with valuable information to work on. You have done a formidable job and our whole community will be grateful to you.


  17. CateringNo Gravatar
    Feb 17, 2011

    I am extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it’s rare to see a great blog like this one nowadays..


  18. dentysta warszawaNo Gravatar
    Feb 18, 2011

    hello there and thank you for your information – I have certainly picked up anything new from right here. I did however expertise some technical issues using this site, as I experienced to reload the website a lot of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Anyway I’m adding this RSS to my e-mail and could look out for a lot more of your respective interesting content. Ensure that you update this again very soon..


  19. Przeprowadzki WarszawaNo Gravatar
    Feb 21, 2011

    Thank you for your post. I like it! Greetings!


  20. Aman Prakash MohlaNo Gravatar
    Oct 30, 2011

    Great Post. Neatly explained all the concepts. Keep doing the good work


  21. Teisha BownNo Gravatar
    Jan 02, 2012

    I have been exploring for a little bit for any high-quality articles or blog posts on this kind of house . Exploring in Yahoo I eventually stumbled upon this web site. Studying this information So i am happy to show that I’ve a very just right uncanny feeling I found out just what I needed. I so much indisputably will make certain to do not forget this website and give it a glance regularly.


  22. pakar digital marketingNo Gravatar
    Feb 09, 2012

    Thank you for sharing superb informations. Your site is so cool. I’m impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found just the information I already searched all over the place and simply couldn’t come across. What a great web site.

Trackbacks/Pingbacks

  1. Tweets that mention HashMap in Java | TechArtifact -- Topsy.com
  2. PimpThisBlog.com

Leave a Reply