Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java HashMap and Its Views

1. Introduction to Java HashMap

The Java HashMap is a special type of Collection. Unlike other collection classes, it represents Key-Value pairs. In this example, we will explore Map Interface and its functions. Then, we will move on to the implementing class HashMap. We will see how HashMap stores collection items using Key-Value pairs and how it offers different views on its internal data store.

2. Map Interface

The Map Interface is a contract for representing the Key-Value pairs. Here, Key will refer to the Value. So, when we have Key in hand, we can get the Value. For example, we can use the Product Id as Key and store the entire Product object as a Value. Since key is the one which gets you to the Value, the Key must be unique, and value may not be. The put and get are the frequently used methods of the Map interface. We will learn about them when we use its implementing class HashMap in this example.

3. Java HashMap & Key-Value Pair

The HashMap class implements the Map Interface and hence it represents the data items as Key-Value pairs. Have a look at the picture below:

Key-Value Pair of HashMap
Key-Value Pair of HashMap

Here, we can see a HashMap is holding three Key-Value pairs. Each Key is pointing to a Value. A Key-Value pair is called as an Entry. So, the above HashMap is having three Entries. In some programming languages, this concept is called a dictionary. Recall, the Value can have duplicate. Say, for example, it is possible for the Key3 to point to a Value, value 1, which is a duplicate. But HashMap does not allow the duplicates in Key.

4. Views of HashMap

We can extract three types of view from the Java HashMap. They are:

  1. Key View
  2. Value View
  3. Entry View

Key View contains only Keys and we can retrieve it using the method keyset. The return value is a Set as keys does not allow duplicates.

Value View contains only the Values and we can get it via the method call values. Since HashMap allows duplicates for Values, the return value is a Collection.

The Entry View is the collection of Entries which can be retrieved from the Java HashMap using the entrySet method. As each entry is unique because of the Key, the return value is again a Set.

The below picture shows the three views of the Java HashMap:

Three Views of Java HashMap
Three Views of Java HashMap

5. Using HashMap Methods

5.1 Adding Entries to HashMap Via put Method

While creating HashMap, we can specify the Data Type of Key and Value. In the below code, at Line No 11, we specify the key as Integer and value as String. We store this Java HashMap in a reference, ProductMap. Next, we call put method to add the entries to it. The first param is the Key and the second one is Value. At this stage, our HashMap is ready with six entries.

5.2 Retrieving Entries From HashMap

We can retrieve a Value using the get method. The method takes a Key and returns the associated Value. Below is the code:

Output of the above code snippet:

 5.3 Replace Value Through Key

The replace method of the Java HashMap takes two parameters. First one is the Key which exists in the HashMap and the second one is the new Value which needs to be mapped to the Key. When a Key is found, the method replaces the Value and returns the old mapped Value to the caller. Code snippet is below:


5.4 The containsKey and containsValue Methods

The containsKey method walks through the Keys present in the HashMap and returns true when the Key presents. The containsValue also works the same way. Below code uses both the methods:

5.5 Printing All Keys of Java HashMap

The keySet method of the Java HashMap returns all the keys stored in it. Since the return value is a set, the printKeys custom function takes a Set as an argument. The function then iterates through this Key Set to print it in the console output window. We can see the keySet function is giving us the Key View of the HashMap.

5.6 Printing All Values of Java HashMap

We can get the Value View of the Java HashMap via the values method. Since values in the HashMap are unique it returns a Collection not the Set. The printValue custom function grabs iterator from this Collection and print all the values present in the HashMap. Below is the code:

5.7 Printing Key-Value Pairs of Java HashMap

In the below code, at line 2-3, we get the Key-Value pair by calling the entrySet function on Java HashMap. The entries (Key-Value pair) are unique, as Keys are unique. So, the Entries returned is a Set. Note how we declared the Set reference at line 2. An Entry is a Key and Value combination. The data type of Key is Integer and data type of Value is String. So, we declare Entry as Entry<Integer, String>. The Set is the collection of these Entries and hence the type goes as Set< Entry<Integer, String>>.

In the printEntrySet function, we get iterator and it over the Entry<Integer, String>. The next method of the iterator returns an Entry object, which is a Key-Value pair. We can use getKey method to get the Key and getValue method to get the Value. Our custom method iterates over all the Key-Value pairs in the Java HashMap and prints that in the console output window.


6. Complete Code Example – Java HashMap

7. Watch Java HashMap as YouTube Video

Categories: Java

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.