This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find the value for a particular key in a Hashmap

 
dianna goldfarb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a hashmap that contains Strings (keys) and Dates (values).

Sample hashmap is:

"5100", "2009-06-29"
"2700", "2009-07-04"

If a variable containing "5100" is passed in, I want to get "2009-06-29".

Thank you!
-dianna
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if your map is in variable "map", and "5100" is in variable "key", then result becomes "2009-06-29":

String result = map.get(key);
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your HashMap is returning actual Java Date objects, then you need:
Date result = map.get(key);

Otherwise you will get a class cast exception (and if your HashMap is using generics, you would get a compile time exception).

Check out the HashMap API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic