Q:

What is a map in Java?

A:

Quick Answer

In the Java programming language, a Map is an object that connects keys to values. It works like a mathematical function, so each key can only link to one value, and each key must be unique.

Continue Reading

Full Answer

Maps are similar to dictionaries in other programming languages and are conceptually similar to real-world dictionaries. A Map object allows programmers to create an object with easy-to-understand values. For example, a Map can contain information about a person, and possible keys might include "name," "height," "eye color" and other basic attributes.

Map actually covers three related objects: HashMap, TreeMap and LinkedHashMap. HashMap stores the map as a hash table, which is an efficient means of storing information but doesn't guarantee that information is in order. TreeMap sacrifices some of this efficiency for guaranteed ordering, while LinkedHashMap uses a doubly-linked list for storing information, which ensures ordering and provides better performance in some scenarios.

Maps are part of the Java Collections Framework, which ensures access to a wide range of tools for populating, reading and setting individual elements. This also lets programmers convert Maps into other Collections and make Maps from other types. Java doesn't support multimaps, which are maps with keys that link to multiple values, but it does let programmers use Lists as values, which provides the same functionality.

Learn more about Computer Programming

Related Questions

Explore