A Java Map is an aggregation of values, each of which is associated with exactly one key. The distinguishing feature of a Java Map is that keys are unique and every key is associated with a value. The same value may be associated with two keys but two values can’t have the same key. A key maps to exactly one object. This object may itself be a composite such as a Collection or a String. In Java lingo a pair of key and value is commonly referred to as Entry.
When to Use Java Maps
Java Maps specialize in searching. Anytime you have to implement/use a data structure that is optimized for searching, you should go for Maps.
Java Maps are based on the Table abstract data type (ADT). A Table data structure is designed to optimally store and retrieve a key value pair. Since searching/retrieving is such a fundamental task in computer science, almost all computer languages (except for minimalist languages such as C) provide some equivalent of Maps. Maps are sometimes also referred to as Table or Dictionary in common parlance.
Java Maps in Relation to Collection
Java Maps are not strictly a Collection in the sense that they don’t implement the Collection interface. Still, Map interface provides three collection-views of itself. These collection-views allow you to view the keys, values and entries as Collections.
The three collection-view methods of Map are keySet, entrySet and values. These methods are called collection-view methods because they –
- Return a Collection of either key, value or entry.
- Are backed by the Map, meaning any change made to the underlying Map is reflected in the collections returned by these methods. If the Map is changed while the Collection returned by these methods is being iterated, the results are JDK dependent (or in other words undefined /unspecified in the specs).
These methods are discussed in more details in the next section.
Important Methods of Java Maps
The below list summarizes important methods in Java Maps –
- V get(Object key) -> is the most important method of Map interface. Is used to retrieve/search the Object key in the Map. Returns the value associated with key.
- boolean containsKey(Object key) –> another search method of Map interface. Returns true or false based on whether key exists in the Map or not.
- V put(K key, V value) -> This method inserts a key, value pair in a Map.
- void putAll(Map map) -> Is used to put all key, value pairs of one Map into another.
- Object remove(Object key) -> Deletes an entry (both key and value) identified by key from a Map.
- Set<K> keySet() -> Returns all keys in a Map stored as a Set.
- Set<Map.Entry<K,V>> entrySet() -> Returns all entries (i.e. key value pairs) of a Map as a Set
- Collection<V> values() -> Returns a Collection of all the values stored in the Map.
Constructors in Java Map
A constructor is not part of an interface. Therefore, Java Map interface does not impose any restriction on the constructors a concrete implementation must have. Nevertheless, by convention all concrete implementations of Map must have two constructors –
- No argument constructor – creates an empty Map
- Copy constructor – takes Map as an argument and creates another Map that has all the elements contained in the passed argument.
Note that there are no compile-time checks to enforce the existences of these constructors but all concrete implementations follow this convention. So, if you roll out your own custom implementation be sure to provide these two constructors. Old-timers expect these constructors to be present.
If you plan to work with Java Maps for any significant length of time you should also familiarize yourself with a static sub-interface of Map – Entry. The static Entry interface is defined inside the Map interface and represents a key value pair.
Map.Entry has three important methods –
- K getKey()-> Retrieves the key of a particular entry
- V getValue()-> Retrieves the value of a particular entry
- V setValue(V v) -> Replaces the value of this particular entry with the new value.
Having looked at the Map interface, we will now turn to the popular concrete implementations such as HashMap, TreeMap etc.