• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Result:
m2 value
m4 value
m3 value
Size: 3

The book says HashMap is unordered and unsorted.But for me ,it looks like based on the keys,values are sorted.I am consfused on HashMap.please help me to get out.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure, what made you to think it is sorted. In your code, the values are printed in the order of system.out.println(). You overwrote the value of key m1 and therefore the latest value of m1 is being printed.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how you can infer anything about whether a Map is sorted or not by adding elements to it, and then retrieving those elements by key?
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the modified code for better understanding

The above prints

m2 value
m3 value
m1 value
Size: 4
null
bKing
aGowher
eGowher


[ April 14, 2007: Message edited by: M Krishnan ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishnan,

1)First of all you have defined MyClass without a hashCode or equals.
2)Secondly you are passing MyClass a String and assuming some order.

Are you getting what you are doing ?
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.

1.i just meant,based on the keys (referencevariable m1,m2,m3) that order ,values are sorted.

2.I am trying to work on krishnans modified program.i changed the program in 1.4 format.




But please help me to get the output

m2 value
m3 value
m1 value
Size: 4
null
bKing
aGowher
eGowher

Mine is just printing address(not values)
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiv Mohan,


Originally posted by Shiv Mohan
(i) just meant,based on the keys (referencevariable m1,m2,m3) that order ,values are sorted.



1- You have not overridden equals() and hashCode() methods of the
Obejct class in your class named MyClass. It is recommended that
when you are using your own class as a key, you should give
definition to both the methods to compare when two objects should
be treated equals. What default implementation of the equals() says,
two objects are equal if their reference variable are referring to
the same object on the heap.

So far as your confusion of ordering is concerned: there is no
magic with get() method:
get method takes the object reference(key) and returns the value,
you confusion is running around the same;
you may be thinking that get is returning the objects in the same way
you have stored. But it is nothing like that,
you can try by changing the order:



There is nothing like order in the HashMap. And no order means no sorting
of course.

Regards,
cmbhatt
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic