wood burning stoves 2.0
The moose likes Beginning Java and the fly likes hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashmap" Watch "hashmap" New topic


jason hay

Joined: Sep 24, 2007
Posts: 1
hello, i am having a little trouble in figuring out how to ask this question, but ill give it a go. i understand hashmaps and how they work, for example

<ID, Name>
"1", "John"

the question that I have is if i want to input an ID via a textfield and more details about John via a set of textfields. do i need to have a seperate class for ID, just as I would have to have for John's details.

example outputs:

User ID: 101

User Name: John
User Address: 5 Home Street
User Age: 25

User ID: 102

User Name: Jane
User Address: 7 Camp Ave
User Age: 57

Tamas Jano
Ranch Hand

Joined: Feb 21, 2007
Posts: 55
Hi and welcome!

You can store objects in the map so you don't need to worry about creating another class. You can even use the person's own id (if you choose to implement one in the class) as the key in the map and the object as the value.

Check that getId() returns an Integer and not an int or you could convert it or use new Integer(pers.getId()).
[ September 24, 2007: Message edited by: Tamas Jano ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That last paragraph about using Integer rather than int comes about because Map wants an Object for a key, not a primitive. So until recently this wouldn't compile:

In Java 5 or later, however, the compiler will "autobox" an int like that, creating an Integer on the fly. It makes the code look a lot cleaner, but adds some overhead that's easy to not see.

I'm not always comfortable when a compiler adds convenient magic like that. The doc says you need an Object for a key, but an int works. Code that used to be wrong is now right. Next we'll have sheep mating with pigs (meaning to point out that my concerns may be totally silly.) The whole natural order of the world seems at risk.

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
I agree. Here's the link: http://aspose.com/file-tools
subject: hashmap
It's not a secret anymore!