HashMap Example

Java HashMap is a very useful collection. You can store any object under specific key using Java HashMap. In this example we are going to store String values. Our HashMap will store itcuties girls names as a keys, and under each key a comma separated String object containing itcuties categories will be stored.
Our HashMap looks like this

ITCuties - Java HashMap

ITCuties – Java HashMap

While putting values to a HashMap remember that each time you put a value under the same key it’s going to be overridden. To put a value use the put(key,value) method. This code illustrates this behavior.

   // Create the HashMap
   HashMap<String,String> hm = new HashMap<String, String>();
       
   // Put data
   hm.put("Katie", "Android, WordPress");
   hm.put("Magda", "Facebook");
   hm.put("Vanessa", "Tools");
   hm.put("Ania", "Java");
   hm.put("Ania", "JEE");    // !! Put another data under the same key, old value is overridden
       
   // HashMap iteration
   for (String key: hm.keySet())
       System.out.println(key+":"+hm.get(key));
           

The output is:

Magda:Facebook
Vanessa:Tools
Ania:JEE
Katie:Android, WordPress   

Look at the order of the elements. This is not the order that data was added to the HashMap. You never know what will the order of the elements be in the Java HashMap.

If you want to check if there is a key present in the HashMap use the containsKey method. You can also check if a value is present in the HashMap. For that task use containsValue method. Here is an example.

   if (hm.containsKey("Katie"))
       System.out.println("HashMap contains key 'Katie'");

   System.out.println();
       
   if (hm.containsValue("Cooking"))
       System.out.println("HashMap contains value 'Cooking'");
   else
       System.out.println("HashMap DOESN't contain value 'Cooking'");        

The output is:

HashMap contains key 'Katie'

HashMap DOESN't contain value 'Cooking'   

4 Responses to "HashMap Example"

  1. Justin says:

    Thank you. :)

    Reply
  2. Arjun says:

    I’m trying to create a registration form in android and i want to insert the values into each of the field using hashmap. I did like u said in the video ,but i dont know how to get the values in the next page. Any help will be appreciated.

    Reply
    • itcuties says:

      Hello Arjun,
      In what technology are you developing your form? JSF? Plain JSP?
      Let us know, so and we will try to help.
      itcuties

      Reply
  3. przemelek says:

    Don’t know what I think about whole action, it’s somehow edgy, somewhere near sexist approach, and I’m not sure if I like it.

    But lets say something about code ;-)

    Lines 12-13 in first example would be better in this version:

    for (Map.Entry entry: hm.entrySet())
        System.out.println(entry.getKey()+":"+entry.getValue());
    

    HashMap is cheap but why doing something what isn’t needed? To generate keySet() we need to iterate through whole HashMap, and next by doing hm.get(key) we are repeating for each item searching. In worst case when hashes were created in very bad way we will change from O(n^2) to O(n) ;-)

    For educational purpose line 2 should be probably changed to:

    Map hm = new HashMap(); // we should use diamond operator for Java 7+
    

    It will learn all readers operating on interfaces instead of concrete classes.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply to itcuties Cancel reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>