Java Maps

Maps in Java contain key and value pairs, where the keys are unique and are used to map to their corresponding value. Java has a number of different types of map, out of which two will be discussed here, ‘HashMap’ and ‘TreeMap’.

In order to use maps, ‘java.util.Map’ needs to be imported. Depending on which of the two above types of map are used, a further import needs to be included, ‘java.util.HashMap’ or ‘java.util.TreeMap’.

The difference between these two types of map is that a ‘HashMap’ doesn’t maintain an order of the items within it, whereas a ‘TreeMap’ does, by sorting according to the natural order of the keys. All the keys have to be of the same type, as do the values, but the keys and values don’t need to be the same type as each other, for example, the keys could be strings, whilst the values could be integers.

Below is an example of how both of these types of map are declared, where ‘nameHashMap’ and ‘nameTreeMap’ are the names for the respective maps, with each being able to hold keys of type string and values of type integer. Here the keys are a person’s first name, whilst the values are their ages.

Map<String, Integer> nameHashMap = new HashMap<String, Integer>();
Map<String, Integer> nameTreeMap = new TreeMap<String, Integer>();

Below are examples of how items can be added to these maps and then subsequently displayed in the console using a ‘foreach’ loop. The display in the console incorporates both the key and its corresponding value within a sentence for each key and value pair in the map.

nameHashMap.put("George", 34);
nameHashMap.put("Bob", 52);
nameHashMap.put("Fred", 29);
nameHashMap.put("Andrew", 36);

nameTreeMap.put("George", 34);
nameTreeMap.put("Bob", 52);
nameTreeMap.put("Fred", 29);
nameTreeMap.put("Andrew", 36);

System.out.println("HashMap:");

for (String key: nameHashMap.keySet())
{
   System.out.println(key + " is " + nameHashMap.get(key) + " years old.");
}

System.out.println();
System.out.println("TreeMap:");

for (String key: nameTreeMap.keySet())
{
   System.out.println(key + " is " + nameTreeMap.get(key) + " years old.");
}

The output from the above is where the difference between the two maps can be seen, where the names in the ‘HashMap’ are in no particular order, but in the ‘TreeMap’ they are displayed alphabetically.

HashMap:
George is 34 years old.
Bob is 52 years old.
Andrew is 36 years old.
Fred is 29 years old.

TreeMap:
Andrew is 36 years old.
Bob is 52 years old.
Fred is 29 years old.
George is 34 years old.

As previously mentioned, a map cannot contain duplicate keys. If an attempt is made to add a key that already exists in the map, the existing entry takes on the new value, in effect updating the existing entry.

Removing an entry in a map can be done by using the ‘remove’ method and specifying the key, in this case the name.

nameHashMap.remove("Bob");
nameTreeMap.remove("Bob");

If there is a need to check to see if a key exists within a map, then the ‘containsKey’ method can be used with both a ‘HashMap’ and a ‘TreeMap’. The ‘containsKey’ method is used in the examples below to check for the existence of the name ‘George’ in both the maps and display a message to the console if it is found.

if (nameHashMap.containsKey("George"))
{
   System.out.println("George is in the HashMap");
}

if (nameTreeMap.containsKey("George"))
{
   System.out.println("George is in the TreeMap");
}

Further Reading