From the course: Java Essential Training: Objects and APIs

Unlock the full course today

Join today to access over 24,200 courses taught by industry experts.

Maps

Maps

- [Instructor] The Map interface maps unique keys to values. Here's a map of various fruits and their calories. Popular implementations of the Map interface are HashMap, TreeMap and LinkedHashMap, which you can read more about in the Java docs. In this example, we instantiated our map with HashMap. A map is not technically a collection, meaning it does not inherit from the collection interface but it's still considered part of the collection's framework. Map does not have access to the add method that we've seen in the other data structures. Map has a put method which is used to add the elements. The put method takes two arguments: a key and a value. We use the fruit's name as a key and the calories as the value. This example uses a string as the key and an integer as the value. However, you can use any type of object for either the key or value. Let's print the contents of this map. Notice the elements are…

Contents