• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

HashMap question?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In HashMap, we can get the value if we know the key:

HashMap hm = new HashMap( );
hm.put("java1","eclipse");
hm.put("java2","NetBean");

String value = (Object)hm.get("java1");

How to get the key if we know the mapping value(object) only?


thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

If you really need to do this, you can call iterator() to get an iteration over Map.Entry objects, and look at the value() in each Map.Entry until you find the value of interest, and then look at the key(). This is obviously quite inefficient if you need to do it multiple times; in that case, consider keeping two maps, one in each "direction."
 
watsonyaya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can not figure it out, can you show me a sample?

HashMap hm = new HashMap();
hm.put("java1","NetBean");
hm.put("java2","Eclipse");
hm.put("java3","JBuilder");
hm.put("java4","JDeveloper");
Can you retrieve String "java4" if you know the mapping object "Jdeveloper" only?

thanks
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no easy way to get it (by easy I meant by making a simple method call)

As the values in a hashmap are not distinct, one value may correspond to multiple keys.

You can code a linear search by accessing each elements corresponding to all keys and mark the key(s)
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's semi-psuedocode for Ernest's suggestion. It returns the first key that it finds mapped to the given value.Map.Entry is an inner class of Map and contains a single pairing (key and value). Note that some Maps allow null values.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or simply use a BidiMap implementation from http://jakarta.apache.org/commons/collections
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic