This section provides code and benchmarks for ten unique example implementations which iterate over the entries of a Map<Integer, Integer> and generate the sum of the Integer values. All of the examples have an algorithmic complexity of Θ(n), however, the benchmarks are still useful for providing insight on which implementations are more efficient in a “real world” environment.

  1. Implementation using [Iterator](<https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html>) with [Map.Entry](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html>)
Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry<Integer, Integer> pair = it.next();
    sum += pair.getKey() + pair.getValue();
}
  1. Implementation using [for](<https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html>) with [Map.Entry](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html>)
for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
    sum += pair.getKey() + pair.getValue();
}
  1. Implementation using [Map.forEach](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#forEach-java.util.function.BiConsumer->) (Java 8+)

map.forEach((k, v) -> sum[0] += k + v);

  1. Implementation using [Map.keySet](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#keySet-->) with [for](<https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html>)
for (Integer key : map.keySet()) {
    sum += key + map.get(key);
}
  1. Implementation using [Map.keySet](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#keySet-->) with [Iterator](<https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html>)
Iterator<Integer> it = map.keySet().iterator();
while (it.hasNext()) {
    Integer key = it.next();
    sum += key + map.get(key);
}
  1. Implementation using [for](<https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html>) with [Iterator](<https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html>) and [Map.Entry](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html>)
for (Iterator<Map.Entry<Integer, Integer>> entries = 
         map.entrySet().iterator(); entries.hasNext(); ) {
    Map.Entry<Integer, Integer> entry = entries.next();
    sum += entry.getKey() + entry.getValue();
}
  1. Implementation using [Stream.forEach](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer->) (Java 8+)

map.entrySet().stream().forEach(e -> sum += e.getKey() + e.getValue());

  1. Implementation using [Stream.forEach](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer->) with [Stream.parallel](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/BaseStream.html#parallel-->) (Java 8+)
map.entrySet()
   .stream()
   .parallel()
   .forEach(e -> sum += e.getKey() + e.getValue());
  1. Implementation using [IterableMap](<https://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/IterableMap.html>) from Apache Collections
MapIterator<Integer, Integer> mit = iterableMap.mapIterator();
while (mit.hasNext()) {
    sum += mit.next() + it.getValue();
}
  1. Implementation using [MutableMap](<https://www.eclipse.org/collections/javadoc/8.0.0/org/eclipse/collections/api/map/MutableMap.html>) from Eclipse Collections