Java HashMap example
HashMap is an object that stores both “key/value” as a pairs. In this article, we show you how to create a HashMap instance and iterates the HashMap data.
["mkyong","1000.00"] = ["key","value"]
In above, “mkyong” is the key , “1000.00″ is the value. By given a key “mkyong”, you can get the value “1000.00″.
Example
See full example of using HashMap.
package com.mkyong.core; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Number { public static void main(String[] args) { try { Map mMap = new HashMap(); mMap.put("PostgreSQL", "Free Open Source Enterprise Database"); mMap.put("DB2", "Enterprise Database , It's expensive"); mMap.put("Oracle", "Enterprise Database , It's expensive"); mMap.put("MySQL", "Free Open SourceDatabase"); Iterator iter = mMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry mEntry = (Map.Entry) iter.next(); System.out.println(mEntry.getKey() + " : " + mEntry.getValue()); } mMap.put("Oracle", "Enterprise Database , It's free now ! (hope)"); System.out.println("One day Oracle.. : " + mMap.get("Oracle")); } catch (Exception e) { System.out.println(e.toString()); } } }
Output
PostgreSQL : Free Open Source Enterprise Database MySQL : Free Open SourceDatabase Oracle : Enterprise Database , It's expensive DB2 : Enterprise Database , It's expensive One day Oracle.. : Enterprise Database , It's free now ! (hope)

Simple and nice tutorial, but I have a problem where I need to keep track of number of occurrence of words every time I found that words in text. So I would like to know will HashMap will be good solution for me. Thanks
Great example, but can you comment on the compiler warning about hashmap being a generic type that it should be parameterized?
Sir Pls help me, i stored the keys and Values.. I can get the value from the hash map, bt not in ordered.. i got it shuffled.. So how to get the HashMap values by the order, what i stored..
Good Work, Simple and Clear
This website is cool. Java is cool too. I just finish TPL program, and i used HashMap to store certain things. it makes my work simple and faster. http://vibeink.com
Hi There ! I’m from Indonesia and I’d like to ask how we’d show up the Key if we input the value. imagine I’m making dictionary coding using Map in Java. there my email, please so small reply to my email, I’d appreciate it if U share. Thank you, Mkyong !
Hello @Maritha, if am not mistaken u are asking how to display ur hashmap values? if yes Map.values() will return collection of value in the map. u can use get to return each value by key. http://vibeink.com
Very Good Example,
I have a dout..i am adding key and value to hash map value as arraylist.
(data getting from data base)
i want to display the key values in jsp page.
key — Time interval(24 hours)
Value –(login,uniqlogins)
if record is not there for particular interval then how i assign a key and value(default 0,0).
Ex: Time Interval : 00-23
for 3,4,5,6,7 there is no records for this interval
then i need to add 0,0 to values for that particular key.
please help me thanksin advance…
Hello MKYONG, I am thinking of a network monitoring system as as my final year project
can u give any help as to how to start and what is required (Java)
a network monitoring system ? awesome.
Really nice & simple program to illustrate Hash. Add tweet button. I’ll tweet this!!!
clear and concise example of hashmap :)
Good Example for beginners, though I read couple of article on hashmap I found your example most simple and How HashMap works in Java theory most useful. Thanks
Thanks, good and simple example
gud example….!
good explanation of basic stuff in HashMap. I also found this link worth checking How HashMap works in Java
HashMap is very useful class in java but most people don’t understand difference between HashMap and Hashtable and when to use which , its important to learn correct usage of collection classes. to read about Hashtable vs HashMap see .
Cheers
Javin
hi,
This is very very good website for java and open source tutorials.
thanks
please give me a chat mpdule while using a map please….?
please send your project to my email , will consult and give some comments.