Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JAVA»Java HashMap – How to Get Key from Value

    Java HashMap – How to Get Key from Value

    RahulBy RahulApril 2, 20222 Mins ReadUpdated:April 6, 2022

    Write a Java program to get keys from the hashmap using the value.

    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 keys from a HashMap based on a defined value.

    Get Key for a Value in 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.

    Here is an example Java program to initialize a HashMap and then iterate through all key-pair using for loop.

    Let’s create a file HashMapExample1.java in your system and add the below content.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    import java.util.HashMap;
    import java.util.Map.Entry;
     
    class HashMapExample1 {
      public static void main(String[] args) {
     
        // Define a hashmap
        HashMap<Integer, String> cities = new HashMap<>();
     
        // Adding key pair to hashmap  
        cities.put(101, "Delhi");
        cities.put(102, "New York");
        cities.put(103, "Peris");
        cities.put(104, "Denmark");
     
        // Define value to search key for
        String value = "Peris";
     
        // Iterate through hashmap using for loop
        for(Entry<Integer, String> entry: cities.entrySet()) {
          if(entry.getValue() == value) {
            System.out.println("The Key for '" + value + "' is " + entry.getKey());
            break;
          }
        }
      }
    }

    Save the file and close it.

    Now, compile the Java program and run. You will see the results below.

    Output:
    The Key for 'Peris' is 103

    Get All Key Values in HashMap

    Here is another example showing to get all key values from a Java HashMap.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import java.util.HashMap;
     
    class HashmapExample2 {
      public static void main(String[] args) {
     
        // Define a hashmap
        HashMap<Integer, String> cities = new HashMap<>();
     
        // Adding key pair to hashmap  
        cities.put(101, "Delhi");
        cities.put(102, "New York");
        cities.put(103, "Peris");
        cities.put(104, "Denmark");
     
        // Print all hashmap key pairs
        System.out.println("HashMap: " + cities);
      }
    }

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

    Output:
    HashMap: {101=Delhi, 102=New York, 103=Peris, 104=Denmark}

    Wrap Up

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

    HashMap Java
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous Article5 Methods to Print an Array in Java
    Next Article Java HashMap – How to Get Value from Key

    Related Posts

    How to Install JAVA on Ubuntu 22.04

    Updated:May 16, 20224 Mins Read

    Java HashMap – How to Get Value from Key

    Updated:April 6, 20222 Mins Read

    5 Methods to Print an Array in Java

    Updated:April 21, 20222 Mins Read

    How To Install Java on Debian 11

    4 Mins Read

    How to Install Tomcat 10 on Ubuntu 20.04

    Updated:February 18, 20226 Mins Read

    How to Check Java Version

    2 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.