File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections - HashMap

 
charla Vella
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the previous rancher said, learn how to UseCodeTags <- click this link
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
nir sharma
Ranch Hand
Posts: 72
Eclipse IDE Java Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
charla Vella,
Your post was moved to a new topic.
new thread, so splitting
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic