• Post Reply Bookmark Topic Watch Topic
  • New Topic

fetching key of the hashmap from its value  RSS feed

 
Udayan Kumar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a scenario where in I get values something like this "Continent", "Country", "City" etc.
Based on the value I have to retreive their code (5, 4, 3 etc).

Since we cannot use String with the switch statement.
I am using a Hashmap to store the above values. So ideally i will have to use the String values (Country, City etc) as key & the corresponding codes
(4,3 etc) as values so when I do a getValue i get the values based on the key.
Normally all samples I have seen do not construct a HashMap in that fashion. Its always the code (1,2,3 so on) as key & String objects say ("continent","city","country" so on) as values. So keeping this scenario in mind can I fetch teh corresponding key based on value.
Or should I be resorting to the first option

Do post your thoughts on the same.


Regards,
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If people use the code as the key, then because they need to fetch the value to a specific code. You are right that when you need to fetch the code to a value, your value should be the key of the Map.

If you need both directions, google for "BidiMap".
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to illustrate why you can't fetch by value, what if we built a map like this:

It would be impossible to retrieve by "Omaha" and get a code. In your case you could say that is simply wrong and you won't add Omaha three times, but the authors of Map could not say such data is always wrong so they couldn't handle fetch by value.

As Ilja suggests, you may just need to turn things around:

Now you can fetch by city name.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Udayan can simply do what he was thinking about doing. There is nothing wrong with what he plans. I think its just his way of expressing his thinking is a bit odd because he is not very familiar with Map useage.

I think he is saying each country has its own code. And he has a country-code map. And a city-code map, and a continent-code map.

In that case the 'country' should be the key, and the code should be the value. There is nothing wrong with the number being the value. Eventhough most of the time you tend to see the number as the key because as humans we care more about strings than numbers. Typically.
 
Paul Clapham
Sheriff
Posts: 22507
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the only purpose of this reverse Map is so that a switch statement can be used, then I wouldn't use it at all. I would just write an if-else-else statement instead. Besides half the time I forget to put the stupid "break" statements into my switch statement, so I prefer not to use it anyway.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
If the only purpose of this reverse Map is so that a switch statement can be used, then I wouldn't use it at all. I would just write an if-else-else statement instead.


Depends on the situation. Often the use of a Map makes it much easier to read and change the code.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
If the only purpose of this reverse Map is so that a switch statement can be used, then I wouldn't use it at all. I would just write an if-else-else statement instead. Besides half the time I forget to put the stupid "break" statements into my switch statement, so I prefer not to use it anyway.


Why have you called it a 'reverse Map?'
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!