Jenkov.com
Tutorials About




Java Collections - Map


Last updated: 2014-06-23

The java.util.Map interface represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit different from the rest of the collection types.

Here is a list of the topics covered in this text:

  1. Map Implementations
  2. Adding and Accessing Elements
  3. Removing Elements
  4. Generic Maps
  5. More Details in the JavaDoc

Map Implementations

Since Map is an interface you need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Map implementations in the Java Collections API:

  • java.util.HashMap
  • java.util.Hashtable
  • java.util.EnumMap
  • java.util.IdentityHashMap
  • java.util.LinkedHashMap
  • java.util.Properties
  • java.util.TreeMap
  • java.util.WeakHashMap

In my experience, the most commonly used Map implementations are HashMap and TreeMap.

Each of these Map implementations behaves a little differently with respect to the order of the elements when iterating the Map, and the time (big O notation) it takes to insert and access elements in the maps.

HashMap maps a key and a value. It does not guarantee any order of the elements stored internally in the map.

TreeMap also maps a key and a value. Furthermore it guarantees the order in which keys or values are iterated - which is the sort order of the keys or values. Check out the JavaDoc for more details.

Here are a few examples of how to create a Map instance:

Map mapA = new HashMap();
Map mapB = new TreeMap();

Adding and Accessing Elements

To add elements to a Map you call its put() method. Here are a few examples:

Map mapA = new HashMap();

mapA.put("key1", "element 1");
mapA.put("key2", "element 2");
mapA.put("key3", "element 3");

The three put() calls maps a string value to a string key. You can then obtain the value using the key. To do that you use the get() method like this:

String element1 = (String) mapA.get("key1");

You can iterate either the keys or the values of a Map. Here is how you do that:


// key iterator
Iterator iterator = mapA.keySet().iterator();

// value iterator
Iterator iterator = mapA.values();

Most often you iterate the keys of the Map and then get the corresponding values during the iteration. Here is how it looks:

Iterator iterator = mapA.keySet().iterator();
while(iterator.hasNext(){
  Object key   = iterator.next();
  Object value = mapA.get(key);
}

//access via new for-loop
for(Object key : mapA.keySet()) {
    Object value = mapA.get(key);
}

Removing Elements

You remove elements by calling the remove(Object key) method. You thus remove the (key, value) pair matching the key.


Generic Maps

By default you can put any Object into a Map, but from Java 5, Java Generics makes it possible to limit the types of object you can use for both keys and values in a Map. Here is an example:

Map<String, MyObject> map = new HashMap<String, MyObject>();

This Map can now only accept String objects for keys, and MyObject instances for values. You can then access and iterate keys and values without casting them. Here is how it looks:

for(MyObject anObject : map.values()){
   //do someting to anObject...
}

for(String key : map.keySet()){
   MyObject value = map.get(key);
   //do something to value
}

For more information about Java Generics, see the Java Generics Tutorial.


More Details in the JavaDoc

There is more you can do with a Map, but you will have to check out the JavaDoc for more details. This text focused on the two most common operations: Adding / removing elements, and iterating the keys and values.



Connect with me: Newsletter - Get all my free tips!
     



Close TOC
This website uses cookies to improve the user experience and gather statistics. Our advertisers use cookies too (3rd party cookies), to provide more relevant ads. Continued use of this website implies that you accept the use of cookies on this website. We do not share our cookies with our advertisers, and our advertisers do not share cookies with us.
OK