How to iterate over a hashmap ? How to retrive key pair values seperatly?

14 Mar

HashMap works on principle of hashing, we have put(key, value) and get(key) method for storing and retrieving Objects from HashMap. When we pass Key and Value object  to put() method on Java HashMap, HashMap implementation calls hashCode method on Key object and applies returned hashcode into its own hashing function to find a bucket location for storing Entry object, important point to mention is that HashMap in Java stores both key and value object as Map.

Iterate like this

import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("vijay", new Double(3434.34));
hm.put("ajith", new Double(123.22));
hm.put("rajini", new Double(1378.00));
hm.put("kamal", new Double(99.22));

for (String key : hm.keySet()) {

System.out.println(“KEY    :+ key);

System.out.println(VALUE :+hm.get(key));


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: