Maps in Java

The java.util.Map interface represents a mapping between keys and their values. A maps cannot contain duplicate keys; and each key can maps to at most one value.

Since Maps is an interface, then you need to instantiate a concrete implementation of that interface in order to use it; there are several Maps implementations, and mostly used are the java.util.HashMap and java.util.TreeMap

Iterating Map Entries Efficiently

This section provides code and benchmarks for ten unique example implementations which iterate over the entries of a Maps 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.

Related Article: Iterating through the contents of a Map

  • Implementation using Iterator with Map.Entry
Iterator> it = map.entrySet().iterator();
while (it.hasNext()) {
     Map.Entry pair = it.next();
     sum += pair.getKey() + pair.getValue();
}
  • Implementation using for with Map.Entry
for (Map.Entry pair : map.entrySet()) {
     sum += pair.getKey() + pair.getValue();
}
  • Implementation using Map.forEach (Java 8+)
map.forEach((k, v) -> sum[0] += k + v);
  • Implementation using Map.keySet with for
for (Integer key : map.keySet()) {
     sum += key + map.get(key);
}
  • Implementation using Map.keySet with Iterator
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
     Integer key = it.next();
     sum += key + map.get(key);
}
  • Implementation using for with Iterator and Map.Entry

for (Iterator> entries = map.entrySet().iterator(); entries.hasNext(); ) {
Map.Entry entry = entries.next();
sum += entry.getKey() + entry.getValue();
}

  • The Implementation using Stream.forEach (Java 8+)
map.entrySet().stream().forEach(e -> sum += e.getKey() + e.getValue());
  • Stream.forEach with Stream.parallel (Java 8+) by using Implementation
map.entrySet()
     .stream()
     .parallel()
     .forEach(e -> sum += e.getKey() + e.getValue());
  • Implementation using IterableMap from Apache Collections
MapIterator mit = iterableMap.mapIterator();
     while (mit.hasNext()) {
     sum += mit.next() + it.getValue();
}
  • Implementation using MutableMap from Eclipse Collection
mutableMap.forEachKeyValue((key, value) -> {
    sum += key + value;
});

Performance Tests
Test Environment: Windows 8.1 64-bit, Intel i7-4790 3.60GHz, 16 GB

  • Average Performance of 10 Trials (100 elements) Best: 308±21 ns/op
BenchmarkScoreErrorUnits
test3_UsingForEachAndJava8308 ±21ns/op
test10_UsingEclipseMutableMap309 ±9ns/op
test1_UsingWhileAndMapEntry380 ±14ns/op
test6_UsingForAndIterator387 ±16ns/op
test2_UsingForEachAndMapEntry391 ±23ns/op
test7_UsingJava8StreamAPI510 ±14ns/op
test9_UsingApacheIterableMap524 ±8ns/op
test4_UsingKeySetAndForEach816 ±26ns/op
test5_UsingKeySetAndIterator863 ±25ns/op
test8_UsingJava8StreamAPIParallel5552 ±185ns/op
  • Average Performance of 10 Trials (10000 elements) Best: 37.606±0.790 μs/op
BenchmarkScoreErrorUnits
test10_UsingEclipseMutableMap37606 ±790ns/op
test3_UsingForEachAndJava850368 ±887ns/op
test6_UsingForAndIterator50332 ±507ns/op
test2_UsingForEachAndMapEntry51406 ±1032ns/op
test1_UsingWhileAndMapEntry52538 ±2431ns/op
test7_UsingJava8StreamAPI54464 ±712ns/op
test4_UsingKeySetAndForEach79016 ±25345ns/op
test5_UsingKeySetAndIterator91105 ±10220ns/op
test8_UsingJava8StreamAPIParallel112511 ±365ns/op
test9_UsingApacheIterableMap125714 ±1935ns/op
  • Average Performance of 10 Trials (100000 elements) Best: 1184.767±332.968 μs/op
BenchmarkScoreErrorUnits
test1_UsingWhileAndMapEntry1184.767332.968μs/op
test10_UsingEclipseMutableMap1191.735304.273μs/op
test2_UsingForEachAndMapEntry1205.815366.043μs/op
test6_UsingForAndIterator1206.873367.272μs/op
test8_UsingJava8StreamAPIParallel1485.895233.143μs/op
test5_UsingKeySetAndIterator1540.281357.497μs/op
test4_UsingKeySetAndForEach1593.342 ±294.417μs/op
test3_UsingForEachAndJava81666.296 ±126.443μs/op
test7_UsingJava8StreamAPI1706.676 ±436.867μs/op
test9_UsingApacheIterableMap3289.866 ±1445.564μs/op
  • A Comparison of Performance Variations Respective to Map Size

Usage of HashMap

HashMap is an implementation of the Map interface that provides a Data Structure to store data in Key-Value pairs.

  1. Declaring HashMap
Map myMap = new HashMap();

KeyType and ValueType must be valid types in Java, such as – String, Integer, Float or any custom class like Employee, Student etc..

For Example : Map myMap = new HashMap();
  1. Putting values in HashMap.

To put a value in the HashMap, we have to call put method on the HashMap object by passing the Key and the Value as parameters.

myMap.put("key1", 1);
myMap.put("key2", 2);

If you call the put method with the Key that already exists in the Map, the method will override its value and return the old value.

  1. Getting values from HashMap.

For getting the value from a HashMap you have to call the get method, by passing the Key as a parameter.

myMap.get("key1"); //return 1 (class Integer)

If you pass a key that does not exists in the HashMap, this method will return null.

  1. Check whether the Key is in the Map or not.
myMap.containsKey(varKey);
  1. Check whether the Value is in the Map or not.
myMap.containsValue(varValue);

The above methods will return a boolean value true or false if key, value exists in the Map or not.

Using Default Methods of Map from Java 8

Examples of using Default Methods introduced in Java 8 in Map interface

  1. Using getOrDefault

Returns the value mapped to the key, or if the key is not present, returns the default value

Map map = new HashMap<>();
map.put(1, "First element");
map.get(1);                             // => First element
map.get(2);                             // => null
map.getOrDefault(2, "Default element"); // => Default element
  1. Using forEach

Allows to perform the operation specified in the ‘action’ on each Map Entry

Map map = new HashMap();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.forEach((key, value) -> System.out.println("Key: "+key+ " :: Value: "+value));
// Key: 1 :: Value: one
// Key: 2 :: Value: two
// Key: 3 :: Value: three
  1. Using replaceAll

Will replace with new-value only if key is present

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
map.replaceAll((key,value)->value+10); //{john=30, paul=40, peter=50}
  1. Using putIfAbsent

Key-Value pair is added to the map, if the key is not present or mapped to null

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
map.putIfAbsent("kelly", 50); //{john=20, paul=30, peter=40, kelly=50}
  1. Using remove

Removes the key only if its associated with the given value

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
map.remove("peter",40); //{john=30, paul=40}
  1. Using replace

If the key is present then the value is replaced by new-value. If the key is not present, does nothing.

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
map.replace("peter",50); //{john=20, paul=30, peter=50}
map.replace("jack",60); //{john=20, paul=30, peter=50}
  1. Using computeIfAbsent

This method adds an entry in the Map. the key is specified in the function and the value is the result of the application of the mapping function

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
map.computeIfAbsent("kelly", k->map.get("john")+10); //{john=20, paul=30, peter=40, kelly=30}
map.computeIfAbsent("peter", k->map.get("john")+10); //{john=20, paul=30, peter=40, kelly=30}
//peter already present
  1. Using computeIfPresent

This method adds an entry or modifies an existing entry in the Map. Does nothing if an entry with that key is not present

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
map.computeIfPresent("kelly", (k,v)->v+10); //{john=20, paul=30, peter=40} //kelly not present
map.computeIfPresent("peter", (k,v)->v+10); //{john=20, paul=30, peter=50} // peter present, so
increase the value
  1. Using compute

This method replaces the value of a key by the newly computed value

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
map.compute("peter", (k,v)->v+50); //{john=20, paul=30, peter=90} //Increase the value
  1. Using merge

Adds the key-value pair to the map, if key is not present or value for the key is null Replaces the value with the newly computed value, if the key is present Key is removed from the map , if new value computed is null

Map map = new HashMap();
map.put("john", 20);
map.put("paul", 30);
map.put("peter", 40);
//Adds the key-value pair to the map, if key is not present or value for the key is null
map.merge("kelly", 50 , (k,v)->map.get("john")+10); // {john=20, paul=30, peter=40, kelly=50}
//Replaces the value with the newly computed value, if the key is present
map.merge("peter", 50 , (k,v)->map.get("john")+10); //{john=20, paul=30, peter=30, kelly=50}
//Key is removed from the map , if new value computed is null
map.merge("peter", 30 , (k,v)->map.get("nancy")); //{john=20, paul=30, kelly=50}

LEAVE A REPLY

Please enter your comment!
Please enter your name here