aspose file tools*
The moose likes Beginning Java and the fly likes How to find the value for a particular key in a Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find the value for a particular key in a Hashmap" Watch "How to find the value for a particular key in a Hashmap" New topic
Author

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

dianna goldfarb
Greenhorn

Joined: May 12, 2008
Posts: 2
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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);

[Jess in Action][AskingGoodQuestions]
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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.


SCJP6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find the value for a particular key in a Hashmap