Maps
The collections framework offers one more interface, java.util.Map
, which can be used when dealing with data that is stored as key-value pairs. This type of data storage is becoming more and more relevant as data formats such as JSON are slowly taking over the internet. JSON is a data format that is based on having data stored in the form of nested arrays that always respond to the key-value structure.
Having data organized in this way offers the possibility of having a very simple way to look for data – by means of the keys instead of using, for example, an index, as we would do in an array. Keys are the way we can identify the block of data we are looking for within a map. Let's look at a simple example of a map before looking at alternatives to maps:
The following example shows how to create a simple map and how to print some messages based on the information available within it. The first thing that you will notice in comparison to other interfaces in the...