A Java map is an object that maps keys to values. These keys and values can be of any data type.
A Java map is declared using the keyword Map
. This is followed by angle brackets <>
which contained data types for the keys and values. The first parameter is the data type for the key and the second parameter is the data type for the mapped value. This is followed by the name of the map.
Map <keyType, valueType> mapName;
An example of a Map, named phoneBook
, is given below. It contains keys (names) of data type String
and values (phone numbers) of data type Integer
.
Map <String, Integer> phoneBook;
Since each key can map to at most one value, a map cannot contain identical keys.
A Java map is directly implemented by the following classes:
The Map interface is extended by the SortedMap interface, which is implemented by the TreeMap class.
This hierarchy is shown below:
Free Resources