- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Get key from HashMap using the value
In this article, we will understand how to get key from HashMap using the value. Java HashMap is a hash table based implementation of Java's Map interface. It is a collection of key-value pairs.
Below is a demonstration of the same −
Suppose our input is −
Input HashMap: {Java=8, Scala=5, Python=15} Key: 8
The desired output would be −
The value of Key: 8 is Java
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create a HashMap of integer and string values and initialize elements in it using the ‘put’ method. Step 5 - Define a key value. Step 6 - Iterate over the elements of HashMap, and check if the key previously defined is present in the HashMap. Step 7 - If found, break away from the loop. Step 8 - Display the result Step 9 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.HashMap; import java.util.Map.Entry; public class Demo { public static void main(String[] args) { System.out.println("The required packages have been imported"); HashMap<String, Integer> input_map = new HashMap<>(); input_map.put("Scala", 5); input_map.put("Java", 8); input_map.put("Python", 15); System.out.println("The HashMap is defined as: " + input_map); Integer Key = 8; for(Entry<String, Integer> entry: input_map.entrySet()) { if(entry.getValue() == Key) { System.out.println("\nThe value of Key: " + Key + " is " + entry.getKey()); break; } } } }
Output
The required packages have been imported The HashMap is defined as: {Java=8, Scala=5, Python=15} The value of Key: 8 is Java
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.util.HashMap; import java.util.Map.Entry; public class Demo { static void get_value(HashMap<String, Integer> input_map,Integer Key){ for(Entry<String, Integer> entry: input_map.entrySet()) { if(entry.getValue() == Key) { System.out.println("\nThe value of Key: " + Key + " is " + entry.getKey()); break; } } } public static void main(String[] args) { System.out.println("The required packages have been imported"); HashMap<String, Integer> input_map = new HashMap<>(); input_map.put("Scala", 5); input_map.put("Java", 8); input_map.put("Python", 15); System.out.println("The HashMap is defined as: " + input_map); Integer Key = 8; get_value(input_map, Key); } }
Output
The required packages have been imported The HashMap is defined as: {Java=8, Scala=5, Python=15} The value of Key: 8 is Java
- Related Articles
- Java Program to remove key value pair from HashMap?
- Java Program to Update value of HashMap using key
- Java Program to remove a key from a HashMap
- Get the value associated with a given key in Java HashMap
- Java Program to create a HashMap and add key-value pairs
- Java Program to replace key and value in HashMap with identical key and different values
- Java Program to remove a key from a HashMap only if it is associated with a given value
- How to use null value as key in Java HashMap
- Modify the value associated with a given key in Java HashMap
- Remove value from HashMap in Java
- Get key from value in JavaScript
- Java Program to copy all the key-value pairs from one Map into another
- C++ Remove an Entry Using Key from HashMap while Iterating Over It
- How to get primary key value (auto-generated keys) from inserted queries using JDBC?
- Get key from value in Dictionary in Python

Advertisements