Quantcast
Channel: Java Hashmap: How to get key from value? - Stack Overflow
Browsing all 41 articles
Browse latest View live

Answer by kanaparthikiran for Java Hashmap: How to get key from value?

/** * This method gets the Key for the given Value * @param paramName * @return */private String getKeyForValueFromMap(String paramName) { String keyForValue = null; if(paramName!=null)) {...

View Article


Answer by boy for Java Hashmap: How to get key from value?

I think this is best solution, original address: Java2s import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] argv) { Map<String, String> map = new...

View Article


Answer by user3724331 for Java Hashmap: How to get key from value?

In java8map.entrySet().stream().filter(entry -> entry.getValue().equals(value)) .forEach(entry -> System.out.println(entry.getKey()));

View Article

Answer by Sreedhar GS for Java Hashmap: How to get key from value?

Iterator<Map.Entry<String,String>> iterator = map.entrySet().iterator();while (iterator.hasNext()) { Map.Entry<String,String> entry = iterator.next(); if...

View Article

Answer by Chicowitz for Java Hashmap: How to get key from value?

You could insert both the key,value pair and its inverse into your map structuremap.put("theKey", "theValue");map.put("theValue", "theKey");Using map.get("theValue") will then return "theKey".It's a...

View Article


Answer by Kanagavelu Sugumar for Java Hashmap: How to get key from value?

import java.util.HashMap;import java.util.HashSet;import java.util.Set;public class ValueKeysMap<K, V> extends HashMap <K,V>{ HashMap<V, Set<K>> ValueKeysMap = new HashMap<V,...

View Article

Answer by margus for Java Hashmap: How to get key from value?

public static class SmartHashMap <T1 extends Object, T2 extends Object> { public HashMap<T1, T2> keyValue; public HashMap<T2, T1> valueKey; public SmartHashMap(){ this.keyValue = new...

View Article

Answer by Madhav for Java Hashmap: How to get key from value?

import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Set;public class M{public static void main(String[] args) { HashMap<String,...

View Article


Answer by Jayen for Java Hashmap: How to get key from value?

Use a thin wrapper: HMapimport java.util.Collections;import java.util.HashMap;import java.util.Map;public class HMap<K, V> { private final Map<K, Map<K, V>> map; public HMap() { map =...

View Article


Answer by Fathah Rehman P for Java Hashmap: How to get key from value?

public class NewClass1 { public static void main(String[] args) { Map<Integer, String> testMap = new HashMap<Integer, String>(); testMap.put(10, "a"); testMap.put(20, "b"); testMap.put(30,...

View Article

Answer by Amit for Java Hashmap: How to get key from value?

You can get the key using values using following code..ArrayList valuesList = new ArrayList();Set keySet = initalMap.keySet();ArrayList keyList = new ArrayList(keySet);for(int i = 0 ; i <...

View Article

Answer by André van Toly for Java Hashmap: How to get key from value?

I'm afraid you'll just have to iterate your map. Shortest I could come up with:Iterator<Map.Entry<String,String>> iter = map.entrySet().iterator();while (iter.hasNext()) {...

View Article

Answer by Vitalii Fedorenko for Java Hashmap: How to get key from value?

If your data structure has many-to-one mapping between keys and values you should iterate over entries and pick all suitable keys:public static <T, E> Set<T> getKeysByValue(Map<T, E>...

View Article


Answer by Carl for Java Hashmap: How to get key from value?

Yes, you have to loop through the hashmap, unless you implement something along the lines of what these various answers suggest. Rather than fiddling with the entrySet, I'd just get the keySet(),...

View Article

Answer by David Tinker for Java Hashmap: How to get key from value?

If you build the map in your own code, try putting the key and value in the map together:public class KeyValue { public Object key; public Object value; public KeyValue(Object key, Object value) { ......

View Article


Answer by Chi for Java Hashmap: How to get key from value?

I think your choices areUse a map implementation built for this, like the BiMap from google collections. Note that the google collections BiMap requires uniqueless of values, as well as keys, but it...

View Article

Answer by Vineet Reynolds for Java Hashmap: How to get key from value?

If you choose to use the Commons Collections library instead of the standard Java Collections framework, you can achieve this with ease.The BidiMap interface in the Collections library is a...

View Article


Answer by Jonas K for Java Hashmap: How to get key from value?

It sounds like the best way is for you to iterate over entries using map.entrySet() since map.containsValue() probably does this anyway.

View Article

Answer by wsorenson for Java Hashmap: How to get key from value?

To find all the keys that map to that value, iterate through all the pairs in the hashmap, using map.entrySet().

View Article

Answer by recursive for Java Hashmap: How to get key from value?

There is no unambiguous answer, because multiple keys can map to the same value. If you are enforcing unique-ness with your own code, the best solution is to create a class that uses two Hashmaps to...

View Article
Browsing all 41 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>