Java HashMap detailed tutorial
This is a detailed description about how to use HashMap in Java. Lot of code examples makes it easy to understand it's behaviour.
Tutorial info:
| Name: | Java HashMap detailed tutorial |
| Total steps: | 1 |
| Category: | Basics |
| Date: | 2011-04-13 |
| Level: | Beginner |
| Product: | See complete product |
| Viewed: | 6855 |
Bookmark Java HashMap detailed tutorial
Step 1 - How to use Java HashMap
Java HashMap detailed tutorial
HashMap is an implementation of the Map interface. It means that it stores key-value pairs. Both the key and the value also must be an Object. You can not put primitive types into a HashMap, in this cases use their wrapper classes like Integer. Both the key and the value can be null.
Here is a simple example how to create and fill a HashMap.
HashMap hm = new HashMap();
hm.put("A","Test-A");
hm.put("B","Test-B");
System.out.println(hm.get("A"));
If you use the same key object more than once, then the value field will be overwritten as you can see in this example:
hm.put("A","Test-A");
hm.put("B","Test-B");
System.out.println(hm.get("A"));
hm.put("A","This is an other value");
System.out.println(hm.get("A"));
If you want to get out a key from the HashMap that doesn’t exist then it returns with null. You can also check it with the containsKey() method as presented here:
if (hm.containsKey("NonExistingKey")) {
System.out.println("Key exists");
} else {
System.out.println("Key doesn't exist");
}
Iterating over a HashMap
In cases when you want to process every item from the HashMap you need to call the entrySet() method on the HashMap to obtain a Set of Map.Entry elements. As next step you can get the Iterator of this step and make a loop to visit every item. Here is a simple demonstration how you can iterate over a HashMap:
HashMap hm = new HashMap();
hm.put("A","Test-A");
hm.put("B","Test-B");
hm.put("C","Test-C");
Set hmSet = hm.entrySet();
Iterator iter = hmSet.iterator();
while(iter.hasNext()){
Map.Entry me = (Map.Entry)iter.next();
System.out.println(me.getKey() + " -> " + me.getValue() );
}
Or if you simply need the values and not interested the keys you can use this form:
Remove items from the HashMap
If you want to remove an item from the HashMap then you need to use the remove() method, which requires a key object that you want to remove. You can also remove all of the items using the clear() method.
HashMap hm = new HashMap();
hm.put("A","Test-A");
hm.put("B","Test-B");
hm.put("C","Test-C");
System.out.println("--- The original content ---");
Iterator iter = hm.values().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}hm.remove("A");
System.out.println("--- After one item was removed ---");
iter = hm.values().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}hm.clear();
System.out.println("--- After all items were removed ---");
iter = hm.values().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
Tags: java hashmap, hashmap usage, hashmap example, hashmap code, hashmap iterator
| Java HashMap detailed tutorial - Table of contents |
|---|
| Step 1 - How to use Java HashMap |