Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
map
java map

What is a Java map?

Educative Answers Team

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 ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring