The HashMap class is available under the java.util package. It is pretty similar to HashTable, but the HashMap is unsynchronized and also allows to stole one null key. In this tutorial, you will learn Java examples to get value from a HashMap based on a defined key.

Get Values from Java HashMap

The Entry interface provides a number of methods to access key values from a HashMap. The Entry.getValue() method returns the value based on the provided key. Let’s check with an example. Here we initialize a HashMap and then iterate through all key-pair using for loop. Create a file in your system and add the below content.

Save the file and close it. Now, compile the Java program and run. You will see the results below.

Get All Key Values in HashMap

You can also print all the key values from a Java HashMap using the println() method. Run the below example on your system:

Now, compile and run above Java program. You should see the results as below:

Wrap Up

In this faq, you have learned an example to get the HashMap key based on a value in the Java programming language.