Javatpoint Logo
Javatpoint Logo

Which data structure is used by map?

An unordered collection of key-value pair items is represented by a map data type. Assign the map data type to ports in order to pass map data via transformations. A map element is a key and value pair that corresponds to one object and maps it to another. The Spark engine can read and write map data from complicated files, as well as pass map data through mapping.

A key-value pair is analogous to looking up a word in a dictionary. The key is the word, and the value is the term's meaning or definition. Similarly, the key-value pair in computing associates two bits of data, and one-piece, the "key," may be used to search up the other component, the "value." A map array data type is an ADT (abstract data type) that stores a key-value pair (k-v) in an array. The "key" is a data identifier, while the "value" is the material that is being identified or stored. Each locker at your school has a different key or a different combination lock. If you were to list the locker number and the key for each locker, the "key" would be the locker number and the "value" would be the combination lock or key number that corresponds to each locker. When considering all of the states in the United States and their capitals, the "key" would be the state name and the capital.

The map data type has a few distinguishing characteristics. Because the keys are one-of-a-kind, no duplicate keys are conceivable. For example, if you need to list the names and populations of all the towns in the United States, the map data type cannot be utilised since town names repeat. Washington is repeated in Washington State, New Jersey, and Wisconsin.

When data has previously been recorded into a map data type, it may be viewed in three ways:

  • You can view simply the list of keys,
  • Only the list of values, or both.
  • Both keys and values can be seen as matched pairs.

The map data type is implemented in Java by several Java classes, including HashMap, HashTable, and TreeMap. The keys and values in HashMap are not sorted. The values in TreeMap are arranged by the key. The HashTable is synchronised and does not allow null keys or values.

The map data type is referred to as an associative array because, like an array, it is a collection of values rather than a single value, as an Int or a String is. Furthermore, each distinct key is associated with a value, resulting in an associative array.

Java Code:

Output:

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
1
Name of Student acting as Key of Map::
Nirnay
Age of Student acting as Map of Map::
23
Object added to the Map.

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
1
Name of Student acting as Key of Map::
Alex
Age of Student acting as Map of Map::
26
Object added to the Map.

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
1 
Name of Student acting as Key of Map::
Paul
Age of Student acting as Map of Map::
20
Object added to the Map.

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
1
Name of Student acting as Key of Map::
Merina
Age of Student acting as Map of Map::
32
Object added to the Map.

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
1
Name of Student acting as Key of Map::
Gwen 
Age of Student acting as Map of Map::
22
Object added to the Map.

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
1
Name of Student acting as Key of Map::
Tom
Age of Student acting as Map of Map::
61
Object added to the Map.

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
2
Map::
Name : Alex -> Key : 26
Name : Tom -> Key : 61
Name : Nirnay -> Key : 23
Name : Merina -> Key : 32
Name : Gwen -> Key : 22
Name : Paul -> Key : 20

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
3
Name(key) of Student whose age(value) want to get:: 
Nirnay
Age(value) is:: 23

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
4
The size of the map is:: 6

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
5
Name(key) of Student:: 
Alex
Name(key) Alex is present :: true

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
2
Map::
Name : Alex -> Key : 26
Name : Tom -> Key : 61
Name : Nirnay -> Key : 23
Name : Merina -> Key : 32
Name : Gwen -> Key : 22
Name : Paul -> Key : 20

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
1
Name of Student acting as Key of Map::
Nick
Age of Student acting as Map of Map::
18
Object added to the Map.

Wanna proceed further(Type y or n)? 

y

The options list for Map::

1. To add an object in the Map
2. To print all objects in the Map
3. To get the Value(age) of a certain Key(student)
4. To print the size of Map
5. To verify does a key(student) exist in the Map.
2
Map::
Name : Alex -> Key : 26
Name : Tom -> Key : 61
Name : Nick -> Key : 18
Name : Nirnay -> Key : 23
Name : Merina -> Key : 32
Name : Gwen -> Key : 22
Name : Paul -> Key : 20

Wanna proceed further(Type y or n)? 

n   

In the above code, we have successfully implemented the Map data structure with the help of different functions data is used to perform different functionalities of the map data structure. In the above-written code, we have written a function for adding new data in the map data type and another function to display all of the existing data from the map data type. And one another function that will be used to get the value associated with a particular key which is stored in the map data type. One function that will return the total size of the map data type is also written. And in the last, another function to check whether a particular key is present in the existing map data type or not is also written. Finally, in the end a menu driven program is written for which an object of the hashmap is created which is used to call all these above-written functions in a menu driven sequence.

Hence with the help of this article, we understood what map data type is and how we can implement a simple map data type in the Java programming language. We also saw how we can use different functionalities of the map data type in the Java programming language by calling various inbuilt functions that are provided by the hashmap class.

The map itself is an interface in Java programming language in our example we have used hashmap which is a class that extends this particular interface. Apart from the hashmap class, there are many classes in Java that extend the map interface. some of the examples of the classes extending the map interface in the Java programming language are HashMap, TreeMap, and LinkedHashMap.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA