The Java Collections Framework and those other containers that fall under the heading of "Java Maps" are sometimes lumped together in discussions and sometimes not.
If you've previously looked at the Java Collections Framework you will likely also have seen some references to the idea of a "map" in some form or other, since the underlying idea in all cases is that you have containers in which you can store stuff. So ... what's the difference, and why are maps sometimes regarded as different from the other containers?
The essential difference is this: The components stored in the containers you find in the Java Collections Framework (lists, sets, queues, stacks, and so on) are conceptually "single things" (a number, a string, an object of some kind, and so on). The components stored in a map (which may also be called a "dictionary" or an "associative array") are not, conceptually, single or "atomic" entities, but rather are "key/value pairs" (names and their telephone numbers, books and their authors, and so on).
The components of any kind of collection, including a map, can be accessed by the appropriate kind of iterator. However, another difference between maps and the other containers is the components in a map can also be accessed by key, but not by index as is often the case with the other containers.
This class and interface diagram (png) shows a brief overview of the Java Collections Framework, which is divided into four groups: List, Set, Queue and Map. So this diagram also shows you how maps fit into the overal picture.
And here are two more diagrams that provide more detail: JavaCollectionsAPI and
JavaMapAPI
[As we mentioned above, the Map hierarchy is often regarded as not being
"officially" part of the collections hierarchy.]
And this brief description (docx) should also be helpful in giving you a sense of the more useful and commonly used containers and what considerations arise when you need to choose which container to use for a given application. The kind of question-and-answer scenario described in this document is something one has to go through whenever such decisions have to be made, and it is further illustrated in this "Q & A" diagram.
You will want to keep referring back to the discussion and diagrams here, and perhaps seek alternate presentations out there on the Internet.
java.util.Map<K, V>
An object that maps keys to values. A map cannot contain duplicate keys;
each key can map to at most one value. The Map interface provides
three collection views, which allow a map's contents to be viewed as a set
of keys, collection of values, or set of key-value mappings. The order of
a map is defined as the order in which the iterators on the map's collection
views return their elements. Some map implementations, like the
TreeMap
class, make specific guarantees as to their order;
others, like the HashMap
class, do not.
- clear()
- compute()
- computeIfAbsent()
- computeIfPresent()
- containsKey()
- containsValue()
- entrySet()
- equals()
- forEach()
- get()
- getOrDefault()
- hashCode()
- isEmpty()
- keySet()
- merge()
- put()
- putAll()
- putIfAbsent()
- remove()
- replace()
- replaceAll()
- size()
- values()