Question: If I have an object implementing the `Map` interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?

Will the ordering of elements depend on the specific map implementation that I have for the interface?

Answer: 
In Map one can Iteration over `keys` and/or `values` and/or `both (e.g., entrySet)` depends on one's interested in\_ Like:

1. Iterate through the [`keys -> keySet()`](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#keySet%28%29) of the map:

```
 Map<String, Object> map = ...;

 for (String key : map.keySet()) {
     //your Business logic...
 }

```
2. Iterate through the [`values -> values()`](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#values%28%29) of the map:

```
 for (Object value : map.values()) {
     //your Business logic...
 }

```
3. Iterate through the [`both -> entrySet()`](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#entrySet%28%29) of the map:

```
 for (Map.Entry<String, Object> entry : map.entrySet()) {
     String key = entry.getKey();
     Object value = entry.getValue();
     //your Business logic...
 }

```

Moreover, there are 3 different ways to iterate through a HashMap. They are as below:

```
//1.
for (Map.Entry entry : hm.entrySet()) {
    System.out.print("key,val: ");
    System.out.println(entry.getKey() + "," + entry.getValue());
}

//2.
Iterator iter = hm.keySet().iterator();
while(iter.hasNext()) {
    Integer key = (Integer)iter.next();
    String val = (String)hm.get(key);
    System.out.println("key,val: " + key + "," + val);
}

//3.
Iterator it = hm.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry entry = (Map.Entry) it.next();
    Integer key = (Integer)entry.getKey();
    String val = (String)entry.getValue();
    System.out.println("key,val: " + key + "," + val);
}

```
```
Map<String, String> map = 
for (Map.Entry<String, String> entry : map.entrySet()) {
    MapKey = entry.getKey() 
    MapValue = entry.getValue();
}

```