aspose file tools*
The moose likes Java in General and the fly likes Collections - HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections - HashMap" Watch "Collections - HashMap" New topic
Author

Collections - HashMap

charla Vella
Greenhorn

Joined: Jul 02, 2007
Posts: 2
I cannot quite understand why the output of this code is supposed to be [Book:Sierra&Bates] a b c d e 4 [c,d,b,e]



whereas when i tried to verify the output by coding it the output was: nullnullnullnullnull4 [c, d, b, e]
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

hi,
first of all, write code in code tag, makes it easy to understand.
secondly,

should be

then you will get the desired result. May be a misprint or something.

Nir
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

As the previous rancher said, learn how to UseCodeTags <- click this link


[My Blog]
All roads lead to JavaRanch
sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

charla Vella wrote:I cannot quite understand why the output of this code is supposed to be [Book:Sierra&Bates] a b c d e 4 [c,d,b,e]



whereas when i tried to verify the output by coding it the output was: nullnullnullnullnull4 [c, d, b, e]


As nir sharma suggested the change it compiles and gives the output as - a b c d e 4 [c, d, b, e]
But, i did not understand FIRSTLY that, when we print, hm.size() why is it showing size=4 and why not size=3, as there are 2 key entries having null as an entry AND SECONDLY that when we print hm.values() why does it print the values in a random order any why not in the manner the values were originally entered as- [b,c,d,e]. It would be good if nir sharma,Christophe Verré or someone who knows the reason could explain this doubt of mine with a reason.Waiting for your reply. Thanks


Always believe in yourself
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

why is it showing size=4 and why not size=3, as there are 2 key entries having null as an entry

Two keys have same value, that is 'null', so in hashmap the first value is replaced by the second with the same key.

why does it print the values in a random order any why not in the manner the values were originally entered as- [b,c,d,e]

From javadocs hashmap
"class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time".

hope this helps.
sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

nir sharma wrote:
why is it showing size=4 and why not size=3, as there are 2 key entries having null as an entry

Two keys have same value, that is 'null', so in hashmap the first value is replaced by the second with the same key.

why does it print the values in a random order any why not in the manner the values were originally entered as- [b,c,d,e]

From javadocs hashmap
"class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time".

hope this helps.


Thanks Nir Sharma. Actually, i am preparing for OCJP 6 exam and i have finished reading Head First JAVA and i have also given few practice tests from a Java Simulator software. I have to give this OCJP 6 this time but i just want to ensure that i am fully prepared before i give my actual exam. If you guys could suggest me some tips and tell if i am going in the right way. Should i code or first or should i start giving practice tests as i am doing and if i get stuck with some concept look for the SOLUTION there and there.
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

Well, you can go through the 'certification thread' where you can find many pointers from experts here. You can even post your query there or create a thread.
My suggestion will be to keep giving the exams and cover one topic at a time. Write as many programs as you can and clarify any doubt you have related to concepts.
sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

nir sharma wrote:Well, you can go through the 'certification thread' where you can find many pointers from experts here. You can even post your query there or create a thread.
My suggestion will be to keep giving the exams and cover one topic at a time. Write as many programs as you can and clarify any doubt you have related to concepts.

Thanks Nir Sharma. Have you given OCJP 6 or are you planning to ?
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

sarvesh dikonda wrote:
Have you given OCJP 6 or are you planning to ?

Not yet, and no plans as of now.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

charla Vella,
Your post was moved to a new topic.
new thread, so splitting
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections - HashMap