Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

JAVA HashMap get keys by values

    Map<String, String> map = new HashMap<String, String>();
    
    map.put("abc", "123");
    map.put("xyz", "456");
    
    for(Entry<String, String> entry : map.entrySet()) {
        if(entry.getValue().equalsIgnoreCase("456")) {
            System.out.println(entry.getKey());
        }
    }
Comment

get key from value hashmap

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

class Main {
  public static void main(String[] args) {

    // create a hashmap
    HashMap<String, Integer> numbers = new HashMap<>();
    numbers.put("One", 1);
    numbers.put("Two", 2);
    numbers.put("Three", 3);
    System.out.println("HashMap: " + numbers);

    // value whose key is to be searched
    Integer value = 3;

    // iterate each entry of hashmap
    for(Entry<String, Integer> entry: numbers.entrySet()) {

      // if give value is equal to value from entry
      // print the corresponding key
      if(entry.getValue() == value) {
        System.out.println("The key for value " + value + " is " + entry.getKey());
        break;
      }
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java enum 
Java :: How do you count characters in a string array in Java? 
Java :: java date add hours 
Java :: how to check if user is logged in firebase android and then load another activity 
Java :: how to subtract localdatetime in java 
Java :: Android: remove shadow from bottom navigation 
Java :: how to call a function in java 
Java :: difference between class and interface 
Java :: keytool error: java.lang.Exception: Key pair not generated, alias <demo already exists 
Java :: How to check if a string is in alphabetical order in java 
Java :: add a value to a list java in java hashmap 
Java :: how to convert arraylist to iterable in java 
Java :: swing getsource 
Java :: Looping Through Array Elements Java 
Java :: static import java 
Java :: primefaces download file 
Java :: convertir string a char java 
Java :: convert calendar to date java 
Java :: java heckj object lock 
Java :: java structure example 
Java :: Java Hashmap Add Elements 
Java :: on item click listener for recyclerview adapter 
Java :: exception in thread "main" java.lang.unsupportedclassversionerror: has been compiled by a more recent version of the java runtime (class file version 55.0), this version of the java runtime only recognizes class file versions up to 52.0 
Java :: java check if string contains multiple words 
Java :: queue and stack reader 
Java :: radix sort java 
Java :: how to find the length of an array in java 
Java :: build libgdx jar 
Java :: mobile network ip address permission android 
Java :: how to convert character into operator 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =