What is The Map Interface in Java with Examples?

Büşra Bozgöz
Codimis
Published in
4 min readNov 8, 2022

The Map interface is a member of the Java Collection framework.

To understand how Map interfaces work, let’s briefly recall arrays. Each element in the array has a respective index, and we access data through the index. Arrays do the indexing, not maps. This feature distinguishes the Map interface from Lists. That is to say, lists have indices and elements, but maps have keys and values.

We can actually think of the Map interface as a function in the full sense of the word. In parallel, we will try to explain the rules of the Map interface through the definition of a mathematical function. Firstly, what is a function? A function is defined as a relationship between a set of inputs that each have one output. In simple words, a function is a relationship between inputs where each input is related to exactly one output. Every function has a domain and a codomain, or range. A function is generally denoted by f(x), where x is the input. A function is usually represented as y = f(x). Let’s assume that the Map interface is also a function, keys are inputs, and we express it as a definition set (such that Key = x). The values are the outputs of this function, and we express it as a set of images (such that Value = y). There are two conditions for a function to be defined. These two rules are, respectively:

1- Every element in the first set will match an element in the second set. This Map interface functions similarly. We mean that each key must match a value. Therefore, no key can be defined without a value, and the number of keys must be equal to or less than the number of values. For this reason, a map cannot contain duplicate keys, so we can say that the keys of the map are unique.

2- No element of the first set will match more than one element in the second set. Like the first condition, this also applies to map interfaces. We can clarify that each key can only match one value.

As a result, keys hold a sequence number. Values have a name. The Java platform contains three purpose Map interface implementations: HashMap, TreeMap, and LinkedHashMap. Their behavior and performance are precisely analogous to HashSet, TreeSet, and LinkedHashSet, as described in the Set Interface section.

Type shown as <K,V> generic type, so key and value do not have to be of the same data type.

The Map interface includes methods for operations such as put, get, remove, containsKey, containsValue, size, and empty, putAll and clear, keySet, entrySet.

HashMap : This implementation provides all of the optional Map interface operations, and permits null values and the null key. This class makes no guarantees as to the order of the output.

Now, let’s make a simple example of the Map interface and the HashMap class. First, let’s add the Map and HashMap libraries in the Java util to our project.

import java.util.HashMap; 
import java.util.Map;

Now, we are going to give an example for this subject. Let’s examine what kind of features you offer us with this example.

Input-1
Output-1

HashMaps does not guarantee that elements will output in the order they were entered. We can print values in the same way. In addition to that, we can print values in the same way.

//Print value
for (String k: numberOfEmploye.values()){
System.out.println(k);
}

LinkedHashMap: LinkedHashMap, on the other hand, allows us to output the entered elements respectively, as in the defined LinkedSet.

The LinkedHashMap class avoids the indefinite ordering of the HashMap class, allowing access to the elements of the collection in a predictable order. Of course, we can also achieve this access order by converting the HashMap structure to the TreeMap structure. But, generally, the complexity of setting up the TreeMap structure is higher.

TreeMap: Let’s remember that the Comparator interface allows us to sort by our own algorithm. By using this interface in maps, we can manually get the output we want. In other words, TreeMaps allow us to make our own sorting using this interface.

As a notable example of the use of maps, we can give you the following example :

Let’s consider a university where every professor has courses to teach. A professor can teach more than one course, but each course has only one professor. We can choose a map if we want to store courses and professors in a data structure. Courses are keys, and professors are values on the map.

--

--