Answer by kmj15_23 for Java Hashmap: How to get key from value?
I've been developing a JavaFX application, and this is a problem that I encounter relatively frequently. The best way that I have found to get the desired result is to use a for each loop to iterate...
View ArticleAnswer by Jasur Hakim for Java Hashmap: How to get key from value?
You can also do that work:First : put map (key, value)Second : to update the key you need to remove expressionThird : and put a new key with the oldValue
View ArticleAnswer by Jafar Sadik for Java Hashmap: How to get key from value?
Found too many answers. Some were really great. But I was particularly looking for a way, so that I can get the value using loops.So here is finally what I did:For a HashMap 1-to-1 relation:...
View ArticleAnswer by Shashank Bodkhe for Java Hashmap: How to get key from value?
Simplest utility method to fetch a key of a given value from a Map:public static void fetchValue(Map<String, Integer> map, Integer i){ Stream stream = map.entrySet().stream().filter(val->...
View ArticleAnswer by smftr for Java Hashmap: How to get key from value?
let see my exampleMap<String, String> mapPeopleAndCountry = new HashMap<>();mapPeopleAndCountry.put("Matis", "Lithuania");mapPeopleAndCountry.put("Carlos",...
View ArticleAnswer by Kaplan for Java Hashmap: How to get key from value?
lambda w/o use of external librariescan deal with multiple values for one key (in difference to the BidiMap)public static List<String> getKeysByValue(Map<String, String> map, String value)...
View ArticleAnswer by AConsumer for Java Hashmap: How to get key from value?
Let the value be maxValue.Set keySet = map.keySet();keySet.stream().filter(x->map.get(x)==maxValue).forEach(x-> System.out.println(x));
View ArticleAnswer by pseusys for Java Hashmap: How to get key from value?
As far as I know keys and values of a HashMap are not mixed when you represent them as arrays:hashmap.values().toArray()andhashmap.keySet().toArray()So the following code (since java 8) should work as...
View ArticleAnswer by programmer for Java Hashmap: How to get key from value?
try this:static String getKeyFromValue(LinkedHashMap<String, String> map,String value) { for (int x=0;x<map.size();x++){ if( String.valueOf( (new ArrayList<String>(map.values())).get(x)...
View ArticleAnswer by Markymark for Java Hashmap: How to get key from value?
While this does not directly answer the question, it is related.This way you don't need to keep creating/iterating. Just create a reverse map once and get what you need./** * Both key and value types...
View ArticleAnswer by FrancisGeek for Java Hashmap: How to get key from value?
I think keySet() may be well to find the keys mapping to the value, and have a better coding style than entrySet().Ex:Suppose you have a HashMap map, ArrayList res, a value you want to find all the key...
View ArticleAnswer by Amazing India for Java Hashmap: How to get key from value?
public static String getKey(Map<String, Integer> mapref, String value) { String key = ""; for (Map.Entry<String, Integer> map : mapref.entrySet()) { if...
View ArticleAnswer by Balasubramanian Ganapathi for Java Hashmap: How to get key from value?
You can use the below:public class HashmapKeyExist { public static void main(String[] args) { HashMap<String, String> hmap = new HashMap<String, String>(); hmap.put("1", "Bala");...
View ArticleAnswer by Batman for Java Hashmap: How to get key from value?
If you want to get key from value, its best to use bidimap (bi-directional maps) , you can get key from value in O(1) time.But, the drawback with this is you can only use unique keyset and...
View ArticleAnswer by Manu Bhat for Java Hashmap: How to get key from value?
My 2 cents. You can get the keys in an array and then loop through the array. This will affect performance of this code block if the map is pretty big , where in you are getting the keys in an array...
View ArticleAnswer by ABHI for Java Hashmap: How to get key from value?
Decorate map with your own implementationclass MyMap<K,V> extends HashMap<K, V>{ Map<V,K> reverseMap = new HashMap<V,K>(); @Override public V put(K key, V value) { // TODO...
View ArticleAnswer by DN2048 for Java Hashmap: How to get key from value?
For Android development targeting API < 19, Vitalii Fedorenko one-to-one relationship solution doesn't work because Objects.equals isn't implemented. Here's a simple alternative:public <K, V>...
View ArticleAnswer by phani for Java Hashmap: How to get key from value?
Using Java 8:ftw.forEach((key, value) -> { if (value.equals("foo")) { System.out.print(key); }});
View ArticleAnswer by Ashwin for Java Hashmap: How to get key from value?
for(int key: hm.keySet()) { if(hm.get(key).equals(value)) { System.out.println(key); }}
View ArticleAnswer by kervin for Java Hashmap: How to get key from value?
It's important to note that since this question, Apache Collections supports Generic BidiMaps. So a few of the top voted answers are no longer accurate on that point.For a Serialized BidiMap that also...
View ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleJava Hashmap: How to get key from value?
If I have the value "foo", and a HashMap<String> ftw for which ftw.containsValue("foo") returns true, how can I get the corresponding key? Do I have to loop through the hashmap? What is the best...
View Article