wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes storing and retrieving items in a HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "storing and retrieving items in a HashMap" Watch "storing and retrieving items in a HashMap" New topic
Author

storing and retrieving items in a HashMap

chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
The following code is for a HashMap I've created, for an application I'm working on.

My question is, I know I can combine the desc and quantity and make them one of value in the HashMap, but how do I retrieve them as seperate values.
For instance if I want to display description in one textfield, and the quantity in another.
I hope my question makes sense, I'll clarify if i can
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
My question is, I know I can combine the desc and quantity and make them one of value in the HashMap, but how do I retrieve them as seperate values.
One way to do it is to store a collection of two elements as the entry in the map. For example:

So, when you retreive a value, it contains both description and quantity, but in two different holders.
The other way is to store the object itself in the map, instead of its properties:

When you retreive the object, simply query it for anything you want.
[ October 19, 2003: Message edited by: Eugene Kononov ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Since you know three things about a part consider a Part object.

Now you can build a Part object and put it in the map.

And it's easy to pass all three bits of information about a part around together. Did that all make sense?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Thank you both for your suggestions, ideas. I had already created a class Part for gets and sets, and hadn't even thought about using it that way. I appreciate the advice.
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Back again with new questions,
I used the idea of creating a Part which would contain partNumber, description, quantity. I then put it into a HashMap like this

so now I've got a map, myMap with a key value of parNumber and a, as an object.
How do I get the values out of a. Say I want to just get the description.
I tried the following, but it's not quite right

The problem I run into is that, I can get the program to tell me the part exists, but it won't display the description, and quantity of the current items...
I hope I'm clear
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Chris
You need to have a look at the get method of Map. The containsKey method tells you that your map contains an object stored against that key, but it does not return that object to you for manipulation. You could actually do away with the call to containsKey altogether - just call get, with the key as an argument. This will then return either the object that you want (you can then extract the relevant data from it), or null if there is no corresponding object in the map. Your conditional statement will therefore check for null against the variable to which you assigned the result of get.
Hope this helps
Michael


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Ok, I looked at the Map interface in the Java API, and have come up with this, so far.

This works to check for null values, which is good for checking if the part is in the map. But I'm still having problems with getting the information to display properly. (to be honest I'm starting to get a headache from looking at this). I tried the following, but it only returns the last value added...

I did a System.out.println(myMap.entrySet()) for it and I get this
1=Part@1a52fdf.... which makes no sense to me, but atleast I know it's not empty...
Still confused
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Whoo hooo, ok I figured it out. Thanks for any and all advice that pushed me in the right direction. I just needed 1 line to make it work

It took me awhile....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: storing and retrieving items in a HashMap
 
Similar Threads
array with elements of different types?
Map as a key
Jackcess
context.setAttribute (object)
Would a HashSet be a way to solve this problem?