• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashmap

 
jason hay
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


cheers
 
Tamas Jano
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic