• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is the hashmap in java not sorted?

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,
its great to be here after so long

a question is coming to my mind
"why is the hashmap not sorted? "

I know that tree map is provided for sorting but
still I want to know why hashmap was made such that it does not sort ....

were there any technical problems? or some limitations?

please enlighten

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know why its so, the designers would have a answer.
But my take is: Hashmap is mainly used for storing key-value pairs and there is no intention of keeping the order- be it insertion order or some sorting order. So that way Hashmap is kept simple, if one wants more features choose a better data structure (Collection)
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Kharkar wrote:
...
still I want to know why hashmap was made such that it does not sort ....
...


As a user of Hashmap do you really care for their order? You are mostly concerned with getting your data in O(1) time.
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:As a user of Hashmap do you really care for their order? You are mostly concerned with getting your data in O(1) time.



Agreed. BTW, is it even possible to sort any set of generic objects, and still maintain an O(1) ? Unless someone invented something new recently, I don't think so.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic