Trusted answers to developer questions

What is a Java map?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

A Java map is an object that maps keys to values. These keys and values can be of any data type.

Declaration

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.

Java map hierarchy

A Java map is directly implemented by the following classes:

  • HashMap
  • HashTable
  • LinkedHashMap

The Map interface is extended by the SortedMap interface, which is implemented by the TreeMap class.

This hierarchy is shown below:

RELATED TAGS

java
map
java map
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?