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 Article