This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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
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 ]
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