Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java HashMap Example

  1. /*
  2. Java HashMap example.
  3. This Java HashMap example describes the basic operations performed on the HashMap.
  4. */
  5.  
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8.  
  9. public class HashMapExample{
  10.    
  11.     public static void main(String args[]){
  12.        
  13.         // constructs a new empty HashMap with default initial capacity        
  14.         HashMap hashMap = new HashMap();
  15.        
  16.         /*
  17.        
  18.         To specify initial capacity, use following constructor        
  19.         HashMap HashMap = new HashMap(100);
  20.        
  21.         To create HashMap from map use following constructor        
  22.         HashMap HashMap = new HashMap(Map myMap);
  23.        
  24.         */
  25.        
  26.         hashMap.put("One", new Integer(1)); // adding value into HashMap
  27.        
  28.         hashMap.put("Two", new Integer(2));
  29.        
  30.         hashMap.put("Three", new Integer(3));
  31.        
  32.         /*
  33.         IMPORTANT : We CAN NOT add primitives to the HashMap. We have to wrap it into
  34.         one of the wrapper classes before adding.
  35.         */
  36.        
  37.         /*
  38.        
  39.         To copy all key - value pairs from any Map to HashMap use putAll method.
  40.         Signature of putAll method is,        
  41.         void putAll(Map m)
  42.        
  43.         */
  44.        
  45.         //get number of keys present in the HashMap        
  46.         System.out.println("HashMap contains " + hashMap.size() + " key value pairs.");
  47.        
  48.         /*
  49.        
  50.         To check whether HashMap is empty or not, use isEmpty() method.        
  51.         isEmpty() returns true is HashMap is empty, otherwise false.
  52.        
  53.         /*
  54.        
  55.         Finding particular value from the HashMap:
  56.        
  57.         HashMap's containsValue method returns boolean depending upon
  58.         the presence of the value in given HashMap
  59.        
  60.         Signature of the containsValue method is,        
  61.         boolean containsValue(Object value)
  62.        
  63.         */
  64.        
  65.         if(hashMap.containsValue(new Integer(1))){
  66.             System.out.println("HashMap contains 1 as value");
  67.         }else{
  68.             System.out.println("HashMap does not contain 1 as value");
  69.         }
  70.        
  71.         /*
  72.        
  73.         Finding particular Key from the HashMap:        
  74.         HashMap's containsKey method returns boolean depending upon the
  75.         Presence of the key in given HashMap
  76.        
  77.         Signature of the method is,        
  78.         boolean containsKey(Object key)
  79.        
  80.         */
  81.        
  82.         if( hashMap.containsKey("One") ){
  83.             System.out.println("HashMap contains One as key");
  84.         }else{
  85.             System.out.println("HashMap does not contain One as value");
  86.         }
  87.        
  88.         /*
  89.         Use get method of HashMap to get value mapped to particular key.        
  90.         Signature of the get method is,        
  91.         Object get(Object key)        
  92.         */
  93.        
  94.         Integer one = (Integer) hashMap.get("One");        
  95.         System.out.println("Value mapped with key \"One\" is " + one);
  96.        
  97.         /*
  98.         IMPORTANT:  get method returns Object, so we need to downcast it.
  99.         */
  100.        
  101.         /*
  102.         To get all keys stored in HashMap use keySet method        
  103.         Signature of the keysSet method is,        
  104.         Set keySet()
  105.         */
  106.        
  107.         System.out.println("Retrieving all keys from the HashMap");        
  108.         Iterator iterator = hashMap.keySet().iterator();
  109.        
  110.         while(iterator. hasNext()){        
  111.             System.out.println(iterator.next());
  112.         }
  113.        
  114.         /*
  115.         To get all values stored in HashMap use entrySet() method.
  116.         Signature of the entrySet() method is,        
  117.         Set entrySet()
  118.         */
  119.        
  120.         System.out.println("Retrieving all values from the HashMap");
  121.         iterator = hashMap.entrySet().iterator();
  122.        
  123.         while(iterator. hasNext()){        
  124.             System.out.println(iterator.next());
  125.         }
  126.        
  127.         /*
  128.         To remove particular key - value pair from the HashMap use remove method.        
  129.         Signature of remove method is,        
  130.         Object remove(Object key)        
  131.         This method returns value that was mapped to the given key,
  132.         otherwise null if mapping not found.
  133.        
  134.         */
  135.        
  136.         System.out.println( hashMap.remove("One") + " is removed from the HashMap.");
  137.        
  138.     }
  139.  
  140. }
  141.  
  142. /*
  143. OUTPUT of the above given Java HashMap Example would be :
  144. HashMap contains 3 key value pair.
  145. HashMap contains 1 as value
  146. HashMap contains One as key
  147. Value mapped with key "One" is 1
  148. Retrieving all keys from the HashMap
  149. Three
  150. Two
  151. One
  152. Retrieving all values from the HashMap
  153. Three=3
  154. Two=2
  155. One=1
  156. 1 is removed from the HashMap.
  157. */
Your rating: None Average: 4.4 (55 votes)
Share this
Anonymous's picture

10x for U !

10x for U !
Anonymous's picture

!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!
Anonymous's picture

nicw code buddy....really

nicw code buddy....really useful
Anonymous's picture

Indeed simple and effective

Indeed simple and effective code example to learn hashmap, but on theory part I really like How HashMap works in Java which is based on inner working of hashmap.
Anonymous's picture

Thanks for this class.Its

Thanks for this class.Its very helpful.I would recommend this to anyone who is trying to get hold of HashMap API
Anonymous's picture

is this true??

is this true??
Anonymous's picture

ya....its very good code to

ya....its very good code to learn on hashmap
Anonymous's picture

completely agree nice and

completely agree nice and easy code that's what I was looking for. for theory pat though I found How HashMap works in Java quite useful.
Anonymous's picture

Thnx it really helpfull

Thnx it really helpfull
Anonymous's picture

i love the way presentation

i love the way presentation was.:)
Anonymous's picture

thank u:)

thank u:)
Anonymous's picture

Thanks buddy!

Thanks buddy!
Anonymous's picture

well commented source code i

well commented source code i love it
Anonymous's picture

well commented source code i

well commented source code i love it
Anonymous's picture

Thanks

Thanks
Anonymous's picture

Thanks its very handy.. :)

Thanks its very handy.. :)
Anonymous's picture

hey hot to creat a array of

hey hot to creat a array of hashmap..for example i want fetch a data from database and store it in a result set object..this may contain any number of rows...i want copy those resultset data to the hashmap...can we do that through hashmap..thanks in advance
Anonymous's picture

Thanks for the example -

Thanks for the example - Ketan
Anonymous's picture

Simple and easy way to

Simple and easy way to understand hashmap. good one
Anonymous's picture

Thank you. Good work.

Thank you. Good work.
Anonymous's picture

Really great tutorial...Love

Really great tutorial...Love it...
Anonymous's picture

Thanks for the good example!

Thanks for the good example!
Anonymous's picture

Well constructed .. looks

Well constructed .. looks neat... superlike :)
Anonymous's picture

Great tutorial Understood it

Great tutorial Understood it in just 15 minutes. SuperLike :)))))))))))))
Anonymous's picture

:)

:)
Anonymous's picture

nice one...:)

nice one...:)
Anonymous's picture

excellent tutorial for

excellent tutorial for beginners! Thanks a lot !!
Anonymous's picture

Good Presentation

Good Presentation
Anonymous's picture

Superb code.... please keep

Superb code.... please keep coding this way...
Anonymous's picture

Should have used Generics as

Should have used Generics as well
Anonymous's picture

Excellent Code to learn

Excellent Code to learn HashMap
Anonymous's picture

thanks u , sweet and simple

thanks u , sweet and simple
Anonymous's picture

tnks man. really really good

tnks man. really really good explanation. cheers from brazil.
Anonymous's picture

Excellent Code to learn

Excellent Code to learn HashMap
Anonymous's picture

thankx it is really helpful

thankx it is really helpful
Anonymous's picture

chimpenav ..........good code

chimpenav ..........good code
Anonymous's picture

ya gr8 code to learn abt

ya gr8 code to learn abt hashmap :) n1..
Anonymous's picture

Thankyou

Thankyou
Anonymous's picture

Well written, Thanks for that

Well written, Thanks for that very clean and understandable code
Anonymous's picture

thank you , it is very

thank you , it is very helpfull
Anonymous's picture

good raess

good raess
Anonymous's picture

thank yu=ou

thank yu=ou
Anonymous's picture

Nice post

Nice post
Anonymous's picture

super cool example

super cool example
Anonymous's picture

Goods.Thanks verymuch

Goods.Thanks verymuch
Anonymous's picture

thank you very useful

thank you very useful tutorial
Anonymous's picture

Nice tutorial, Clear consice

Nice tutorial, Clear consice code which explains the concepts of HashMap in a clean way. I have also added a small tutorial on how HashMap are implemented in the Java Memory Model, internally. You can check it out here
Anonymous's picture

really tacky one should

really tacky one should better showed how to retrieve the elements from hashmap
Anonymous's picture

How does one return a hashmap

How does one return a hashmap from a method ? I am trying this ?? Map get_sales () { HashMap map = new HashMap(); map.put("JAN", "15"); map.put("feb", "27"); map.put("march", "12"); return map; } Map x = get_sales (); // this does not seem to work ? Any ideas on how to implement this will be appreciated.
Anonymous's picture

its very nice and

its very nice and simple.......thanx.

Post new comment