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: 
This is a two part question:

*How to iterate over the entries of a Map* - @ScArcher2 has [answered](https://stackoverflow.com/a/46908/1851302) that perfectly.

*What is the order of iteration* - if you are just using `Map`, then strictly speaking, there are **no ordering guarantees**. So you shouldn't really rely on the ordering given by any implementation. However, the [`SortedMap`](http://docs.oracle.com/javase/8/docs/api/java/util/SortedMap.html) interface extends `Map` and provides exactly what you are looking for - implementations will aways give a consistent sort order.

**[`NavigableMap`](http://docs.oracle.com/javase/8/docs/api/java/util/NavigableMap.html) is another useful extension** - this is a `SortedMap` with additional methods for finding entries by their ordered position in the key set. So potentially this can remove the need for iterating in the first place - you might be able to find the specific `entry` you are after using the `higherEntry`, `lowerEntry`, `ceilingEntry`, or `floorEntry` methods. The `descendingMap` method even gives you an explicit method of **reversing the traversal order**.
Yes, as many people agreed this is the best way to iterate over a `Map`.

But there are chances to throw `nullpointerexception` if the map is `null`. Don't forget to put `null` .check in.

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

```